Finding size of a malloc()ed block (was: Finding names of open files)

David Fiander djfiander at watmath.waterloo.edu
Fri Feb 5 01:23:56 AEST 1988


In article <461 at minya.UUCP> jc at minya.UUCP (John Chambers) writes:
:: : this kind of reminds me of someone who asked how to get the size of a malloc'ed block.
:: : The answer?  "Remember how much you asked for, dimwit!"
:: 
:: Actually, if free() is going to be able to deallocate the space, the
:: dimwit malloc() ought to be salting the size away somewhere, since
:: free doesn't depend on the dimwit programmer telling it how big the
:: block is.
:Of course, a good design would always pass a size along with a pointer.
:Sure, and when you are writing your library sprintf() routine, do you
:want to go out and modify all the calls in all the software in the world
:so that there's a size parameter?  I mean, sprintf() is specified and 
:cast in a huge chunk of concrete by now.  It doesn't include a size field.
:How is a competent software engineer to guarantee that it doesn't overflow
:the caller's array?

The only problem with this is that I usually call sprintf() with a statically
allocated array and, the last time I looked, they _don't_ have sizes associated
with them.  It would be nice to be able to find out the size of a malloc()ed 
block, but first we have to be able to figure out if the pointer we have
actually points at something from malloc().

-- 
It was one of themselves, one of their own prophets, who said,
"Cretans were never anything but liars, dangerous animals and lazy": and
that is a true statment.
	- Titus 1:12

UUCP  : {allegra,ihnp4,decvax,utzoo,clyde}!watmath!djfiander
CSNET : djfiander%watmath at waterloo.CSNET



More information about the Comp.unix.wizards mailing list