sizeof on a word-oriented machine

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Sat Nov 18 06:50:19 AEST 1989


In article <11135 at riks.csl.sony.co.jp> diamond at ws.sony.junet (Norman Diamond) writes:

| 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.

  This is the problem: 80 aren't needed, 2*37 are needed. And the sizeof
the first array will always be 37, because that's how long you declared
it to be. Arrays are not ever padded. Struct and unions may be.

  The sizeof an array of N elements will be N times the size of a single
element, no more or less.
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.std.c mailing list