[comp.std.c] Malloc and multiple data objects

tada@athena.mit.edu (Michael J Zehr) (06/02/90)

I've heard various ideas about mallocing 2d arrays without doing a
separate malloc for each row (malloc the space for all the array, then
malloc space for an array of pointers into each row).  I have a question
about this. Is there any way to portable malloc space for more than one
data object at once? I.e. if you want 10 rows of 20 ints, can you:

malloc(10 * size_of(int*) + 10*20*sizeof(int));

and then use it as two arrays, one of int * and one of int?

Is there any way of ensuring proper alignment for the start of the
second array?  (i know there are ways that are likely to work on all
machines, like starting the second array on a boundary where the value
of the pointer is evenly divisible by the size_of an int, but i'm
wondering if anything is guaranteed by the standard about this.)

-michael j zehr

PS. please don't tell me what works or doesn't work on a particular
machine... i'm only interested in something that's guaranteed portable.