Mail Archives: djgpp-workers/1999/05/20/19:16:59
Alain Magloire wrote:
>
> Bonjour
>
> > Years ago when I worked with Microsoft C/C++, I used a function
> > called _msize(). Its purpose was to return the size of a block
> > of memory allocated on the heap. I found this very handy when
> > tracking memory allocations and, especially, leaks and pointer
> > misuse.
>
> 8-)
> Sorry I could not help smiling. I just finish implementing an allocator
> for an RTOS, and usually when people ask me for ways to get back the
> size, it is for all the wrong reasons (I'm not saying that you
> don't have some valid ones)
>
> Most of the time the information block is hold in an offset, -1 of the pointer.
> So p = malloc(40), you should get the size by moving back 1 and casting
> to the appropriate struct. Ususally the size will be the
> last field in the structure so you don't have to guess the offset.
> So something like this .. might work.
It looks like the int at offset -1 indeed. Note that the low bit
appears to be used for something else; you should mask it to 0.
<plug>
If you have memory bugs, you could try my YAMD malloc debugger. It
still has a few problems but should work fine for most things
(especially if they are plain C). See:
http://www.cartsys.com/eldredge/n/yamd/
</plug>
--
Nate Eldredge
nate AT cartsys DOT com
- Raw text -