get size of malloc'd object

Doug Gwyn gwyn at brl-smoke.ARPA
Sun Jun 15 16:11:52 AEST 1986


In article <165 at daisy.UUCP> misha at daisy.UUCP (Mike Umansky) writes:
-	I have a need in my application to find the size of a
-	previously malloc'd object (maybe an array or structure).
-	For example, one routine may allocate some array or just
-	one big chunk of memory and pass a pointer to the chunk to
-	another routine.  This other routine now needs to know the
-	size of the area pointed to by this pointer.  Also, this
-	other routine has no idea about the structure of this area
-	and doesn't even care.  Now, the question, how can this
-	other routine get the size of the area pointed to by passed
-	pointer.  Would (sizeof(*ptr)) work or will it just give
-	me the size of the pointer type?  Another possibility would
-	be to write a routine which would scan malloc's allocation
-	array and try to match the pointers; then the size is
-	easily obtained.

Sorry to be the bearer of bad tidings, but there is NO reliable
way to do what you're attempting.  malloc() may even have allocated
more space than you asked for, but in any case there's no portable
way to find out how much.  You'll just have to keep track of the
size of your malloc()ed objects yourself.



More information about the Comp.unix mailing list