Mail Archives: cygwin/2002/03/06/23:31:28
There can be multiple causes for this behavior depending upon the
implementation of the C compiler. In all cases, you are over-writing an
area of storage allocated for the contents of the *a pointer. This area
may be in a initialized data segment (usually read-only), in a data
segment, or even the stack! It varies between compiler. In any case
the string that is being copied is larger than what was initially
allocated to *a. Since you got a segmentation violation, it is safe to
rule out the fact that the data allocated was NOT in an initialized data
area as that would likely generate a storage protection violation (if
there is such a thing in MS world).
If you are going to be modifying something, make sure it is pointed to
an area of adequate size either by explicit storage declaration or
malloc().
-paul mcferrin
taoism AT mx9 DOT freecom DOT ne DOT jp wrote:
>
> Hi, gentleman, could you do me a favour?
> I had some trouble in running a C program.
>
> [C source code is]
> ----- from here ----
> #include <stdio.h>
>
> int main() {
> char *a = "I am a teacher";
> char *b = "You are a student";
> printf("string_a = %s\nstring_b = %s\n", a, b);
> copy_string(a, b);
> printf("string_a = %s\nstring_b = %s\n", a, b);
> }
>
> int copy_string(char *from, char *to) {
> while((*to++ = *from++) != '\0');
> }
> ----- end here ----
>
> [Compilation Tool]
> gcc version 2.95.3-5(cygwin)
>
> [Question]
> The compilation is passed, but after running the a.exe, the
> following message appeared and I got a a.exe.stackdump too.
> ------
> string_a = I am a teacher
> string_b = You are a student
> 0 [main] a 1536 open stackdumpfile:Dumping stack trace to a.exe
> stackdump
> Segmentation fault (core dumped)
> ------
>
> [Misc]
> The contents of a.exe.stackdump is the following:
> --------
> Exception: STATUS_ACCESS_VIOLATION at eip=004010F0
> eax=00401049 ebx=00000004 ecx=00401044 edx=00401053 esi=610903E8 edi=00000001
> ebp=0240FE84 esp=0240FE84 program=E:\home\Study\C\a.exe
> cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
> Stack trace:
> Frame Function Args
> 0240FE84 004010F0 (00401044, 00401053, 00401053, 0040108F)
> 0240FEB4 004010C5 (00000001, 1A023684, 0A010008, 00000000)
> 0240FF10 61003AEA (00000000, 0247E798, F08ABC4C, 00000001)
> 0240FF40 61003CBD (00401084, 0247E798, FD37F440, 00000000)
> 0240FF60 61003CFC (00000000, 00000000, FD37F5D0, 00000005)
> 0240FF90 00401153 (00401084, FFFFFFFF, 80430D77, 00000000)
> 0240FFC0 0040103D (0247E798, 00000000, 7FFDF000, 0247FA39)
> 0240FFF0 77E97D08 (00401000, 00000000, 000000C8, 00000100)
> End of stack trace
> ---------
>
> --
> Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
> Bug reporting: http://cygwin.com/bugs.html
> Documentation: http://cygwin.com/docs.html
> FAQ: http://cygwin.com/faq/
--
NOTE*** This email looks it came from MailHole AT insight DOT rr DOT com but in
reality it came from pmcferrin AT insight DOT rr DOT com. If you send
a reply to this message, it *should* get delivered to the
correct place.
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -