Path: utzoo!yunexus!ists!jarvis.csri.toronto.edu!mailrus!uunet!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.std.c Subject: Re: sizeof on a word-oriented machine Message-ID: <1642@crdos1.crd.ge.COM> Date: 17 Nov 89 19:50:19 GMT Article-I.D.: crdos1.1642 References: <11135@riks.csl.sony.co.jp> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: GE Corp R&D Center, Schenectady NY Lines: 19 In article <11135@riks.csl.sony.co.jp> diamond@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@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