sizeof on a word-oriented machine

Karl Heuer karl at haddock.ima.isc.com
Sat Nov 18 04:43:48 AEST 1989


In article <11135 at riks.csl.sony.co.jp> diamond at ws.sony.junet (Norman Diamond) writes:
>Consider a machine where each 4-byte word has an address.
>  char x[37];
>What is sizeof x?

37.  Always.

>If sizeof x is 37 then a user might do:
>  char *two_xs;
>  two_xs = malloc (2 * sizeof x);
>and get screwed because only 76 bytes will be allocated (2 * 37 rounded
>up to a multiple of 4) when 80 are really needed.

Why do you think that 80 are needed?  The second of the two x-sized objects
pointed to by the return value of malloc() will not be word-aligned, but that
should be no more surprising than the equivalent fact about the second
char-sized object obtained from malloc(2*sizeof(char)).

>Is the standard's example wrong?  May it be ignored?

No.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.std.c mailing list