Need debugging version of malloc()/free()

Bennett Todd bet at bent
Sun Jun 11 09:11:06 AEST 1989


My error-checking wrappers library (libbent) does this in its wrappers
around malloc/free/realloc (emalloc, efree, and erealloc, respectively).
I sometimes regret having done this, since as a result of this decision,
the pointers handled by emalloc/efree/erealloc aren't interchangeable
with malloc/free/realloc, whereas the other cookies that are passed
about (file descriptors, FILE pointers, and so forth) are
interchangeable between my e* wrappers and the lower level functions. On
the other hand, I haven't gotten into trouble as a result of mixing yet,
and the additional checking has helped me catch some evil bugs quickly.
My code has emalloc over-allocate by twice the length of a header
structure, which includes a magic number and the allocation length. One
copy is prepended, and one appended, and the pointer to the middle space
is returned. On efree and erealloc the headers and trailers are checked;
on efree they are mangled in a predictable fasion, so that double
freeing can be detected. This catches double freeing, freeing bogus
pointers, and running off either end of the array. I use these in all my
production code. Although they don't catch problems as quickly as some
other more ambitious packages out there, they also don't inflict enough
overhead to make me want to leave them out of production code.

-Bennett
bet at orion.mc.duke.edu



More information about the Comp.unix.wizards mailing list