Path: utzoo!attcan!uunet!husc6!mailrus!ames!netsys!vector!rpp386!jfh From: jfh@rpp386.Dallas.TX.US (The Beach Bum) Newsgroups: comp.lang.c Subject: Re: Tracking memory leaks.. Summary: region size isn't important ... ... count of active regions is. Message-ID: <6514@rpp386.Dallas.TX.US> Date: 14 Sep 88 03:18:37 GMT References: <3950011@eecs.nwu.edu> <164@amsdsg.UUCP> Reply-To: jfh@rpp386.Dallas.TX.US (The Beach Bum) Organization: HASA, "S" Division Lines: 23 In article <164@amsdsg.UUCP> jeff@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@rpp386.Dallas.TX.US) HASA, "S" Division "If the code and the comments disagree, then both are probably wrong." -- Norm Schryer