get size of malloc'd object

J. Eric Roskos jer at peora.UUCP
Tue Jun 17 22:57:04 AEST 1986


	It is really hard to come up which general purpose algorithmic 
	solutions to problems without having even a glimmer of what
	environment you are talking about ...

Sure it is.  Just write your own routine to call malloc; allocate a
sizeof(int) worth of extra space, then store the size of the thing you
malloc'ed in the int at the front of the allocated block, advance the
pointer past the place where your stored the size, and return that as
the pointer to the block you allocated.  The size of the object is then
found in the word preceeding the location pointed to by the object pointer.

Also provide a routine to deallocate the block by backing the pointer up
before calling free().

This approach is portable, simple, and easy to understand.
Also it doesn't require any assumptions about what kind of objects are
being allocated.
-- 
E. Roskos
    "They are a nexus of commercially produced artifact and individual
     message that expresses a dialect central to popular culture."
				-- Sociologist explaining the reason for
				   having Florida postcards.



More information about the Comp.unix mailing list