Tracking memory leaks..

The Beach Bum jfh at rpp386.Dallas.TX.US
Wed Sep 14 13:18:37 AEST 1988


In article <164 at amsdsg.UUCP> jeff at amsdsg.UUCP (Jeff Barr) writes:
>You can allocate space for an extra 'int' in xmalloc, store the size
>yourself at the beginning of the allocated block (being sure to return a
>pointer PAST the 'int'), and then retrieve it in xfree.

the number of bytes being allocated and freed is not important.  you
need to keep a count of the number of memory regions which have been
allocated and freed.

another problem is keeping the allocated region maximally aligned.
if you are using a system where doubles must be aligned modulo
sizeof (double), allocating an int before the region will introduce
alignment (and portability) problems.

the pointer returned by malloc() can be cast to both a (double *) and
a (char *).  so, store the address of the region at the beginning of
the region, then return the address of the double following the
beginning of the region cast (void *).
-- 
John F. Haugh II (jfh at rpp386.Dallas.TX.US)                   HASA, "S" Division

    "If the code and the comments disagree, then both are probably wrong."
                -- Norm Schryer



More information about the Comp.lang.c mailing list