[Tfug] Need help with a C++ algorithm

Brian Murphy murphy+tfug at email.arizona.edu
Thu Mar 15 14:18:01 MST 2007


Quoting Jude Nelson <judecn at gmail.com>:
> You're sure glibc's allocator doesn't have a method for
> determining the size of an allocated block, given a pointer to it?  I would
> be very surprised if it didn't, because then how would free() work?


It does but the interface to the allocator is not exposed.  It's easy to
swap out allocators at build time.  That is how memory debuggers like
Purify work.  They will put a guard zone before and after the allocated
region.  This malloc implementation flexibility is another reason you
have to be really careful if you take Stephen's pointer arithmatic
solution to finding the allocator's data structure that describes the
memory chunk.  It's a neat parlor trick but can you count on your
system using the original K&R style malloc?  You'll have to make that
choice based upon your project's requirements.

Brian

The opinions or statements expressed herein are my own and should not be
taken as a position, opinion, or endorsement of the University of
Arizona.






More information about the tfug mailing list