[LCP]Segmentation Faults when calling free()

Chuck Martin nrocinu at myrealbox.com
Fri Feb 21 06:06:01 UTC 2003


On Thu, Feb 20, 2003 at 08:00:47AM -0800, Bill Rausch wrote:
> Almost always this is caused by some other error in your program 
> involving malloc/free routines.  You've probably overwritten some 
> "bookkeeping" information used by the malloc library.  Debugging 
> requires much patience and a little luck or using a replacement 
> debugging library.

Thanks.  At least now I have an idea what I'm looking for.  By the
way, this program uses wrappers for malloc(), realloc(), and free(),
so there's only one place in the program where all of the malloc()'s
are done, and only one place where all of the free()'s are done.

The malloc() wrapper function adds sizeof(double) to the requested
size before calling malloc(), and then stores a "magic number" at
the beginning of the malloc()'ed space and adds sizeof(double) to
the pointer before returning it to the calling function.  The free()
wrapper first subtracts sizeof(double) from the specified pointer,
then compares the beginning of the space to see that it matches the
"magic number" before calling free(), and if it doesn't, it issues
an error message and aborts the program.  Since I'm not getting the
error message, I assume the "magic number" is correct, so whatever
is causing the problem isn't writing immediately before the space
returned by the malloc() wrapper, but that doesn't mean it isn't
writing something before that.

Chuck




More information about the linuxCprogramming mailing list