C structs & A question about octet

Robert Perlberg perl at rdin.UUCP
Fri Nov 14 03:51:38 AEST 1986


>     head = (three_bytes*)calloc(N, sizeof(three_bytes));
> 
> This wastes N bytes.

It still would even if sizeof(three_bytes)==3.  Malloc allocates
storage that is properly aligned for any data type.  Therefore, even if
you passed 3 instead of 4, malloc would still skip to the next big
boundary.  I suppose that someone could write a calloc that would align
optimally based on the size of the data item being calloc'd, but how
would it know just from the sizeof argument what the proper alignment
for the data type should be?  As far as I know, most, if not all,
implementations of calloc just multiply their arguments and invoke
malloc to allocate the storage.

Robert Perlberg
Resource Dynamics Inc.
New York
{philabs|delftcc}!rdin!perl



More information about the Comp.lang.c mailing list