get size of malloc'd object

J. Eric Roskos jer at peora.UUCP
Sun Jun 29 13:26:15 AEST 1986


>    It seems obvious that the solution to the whole problem is to define a
> structure which contains two fields: the pointer to memory returned by malloc,
> and an int (or long int, or whatever) which contains the size of the block
> requested from malloc.  This is guaranteed to work, as opposed to the many
> alternate implementations that have been proposed and are guaranteed to fail
> on at least some architectures.
>    Note that in a strongly-typed language you would have had to do the right
> thing from the beginning...

Wouldn't malloc() be something of a hole in a strongly-typed language
anyway?  I mean, if you are going to allocate an object in a strongly-typed
language, you should have to allocate an object of a particular type, not
just "a block of memory that is aligned on the worst-case alignment
boundary".

The above "obvious" solution has the shortcoming that either you return a
pointer to the structure (in which case the memory allocator doesn't work
like standard malloc), or you have to have a way to coordinate the structure
with the allocated block of memory: when the user frees a block, you have
to find the structure given only the address of the block -- which is
stored in the structure, but not in the block you're freeing.

>From this discussion I have become firmly convinced (well, semi-firmly)
that this reflects a problem in the C language.  Specifically, C does
allow constraints on alignment, which is reasonable, but there isn't any
way to find out anything about alignment from within the language.
The ANSI C folks should look into that.  Maybe there should be an operator
"alignment" that works the same as "sizeof" but gives the alignment required
for a specified type; and some defined constant (like MAXALIGN) that gives
the worst-case alignment.  For example, how can you write a portable
malloc?  After all, that's really what we've been trying to do here.  You
can argue "malloc is sort of part of the language so it is OK for it to
have to have changes made to it when you port it to a different machine,"
(or put "is too close to the hardware" in place of "is part of the
language", or some similar argument) but the problem is, that seems to give
malloc an unduly privileged status.  What if you do want to write your
own malloc?  Malloc really isn't "part of the language," and there's no
reason why it should have to be considered "too close to the hardware"
except inasmuch as there is this problem with C that you have to tell your
program something you've determined from an outside source that couldn't
be found out from within the language itself.  But of course the
compiler knows about alignment requirements, and we have here a 
demonstrated "need to know," only the compiler won't tell you what it
knows because there's nothing in C to let it do so! 

Well, the above is my argument for adding information about alignment
to the C language.
-- 
E. Roskos



More information about the Comp.unix mailing list