[techtalk] A SIGSEGV problem

bobtfish bobtfish at freaks.screaming.net
Thu May 4 15:54:47 EST 2000


At 19:32 04/05/00 +0530, Deepa Karnad wrote:
>Hi.
>A  g++ program is encountering problems during execution. There are no 
>error messages as it gets linked and compiled. The executable file does 
>not run though. These are the error messages observed:
>'Program received a signal SIGSEGV, segmentation fault'
>OR
>'Segmentation fault (core dumped)'
>  I understand this could be a memory access problem but do not know how 
> to set the problem right.

Yeah, a segmentation fault is caused when you access memory that your 
process is not actually using..
So, as a quick example in c if you do:

char *string;

if ((string=(char *)malloc(sizeof(char)*(strlen("STRING")+1)))==NULL) {
         printf("Could not allocate memory\n");
         abort();
}

strcpy(string, "STRING");

printf("The string is %s\n", string);

free(string);

printf("The string is still %s\n", string);

You will find that you get the first line printed, but a segfault before 
the second.. Remove the free and look at it.. It works :)

free() does not unset the pointer, it just frees the memory area pointed to.

Also if you call free on something you haven't malloced then you get a SEGV 
(Or call free on the data, not the pointer)..
Best thing to do (if you always die in the same place) is to throw lots of 
printfs round the offending code. Then you can see which bit causes the 
SEGV. Alternativly gdb is your friend ;)

Hope this helps

BobTFish
P.S. If this is someone else's program then the above exercise will be 
non-trivial..






More information about the Techtalk mailing list