🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #918

Re: sizeof on a word-oriented machine

#918
From: Karl Heuer
Date: Fri, 17 Nov 1989 17:43
23 lines
1320 bytes
In article <11135@riks.csl.sony.co.jp> diamond@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@haddock.isc.com), The Walking Lint
Technical Details
Message-ID: <15216@haddock.ima.isc.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!usc!snorkelwacker!spdcc!ima!haddock!karl
References: <11135@riks.csl.sony.co.jp>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!usc!snorkelwacker!spdcc!ima!haddock!karl
From: karl@haddock.ima.isc.com (Karl Heuer)
Newsgroups: comp.std.c
Subject: Re: sizeof on a word-oriented machine
Message-ID: <15216@haddock.ima.isc.com>
Date: 17 Nov 89 17:43:48 GMT
References: <11135@riks.csl.sony.co.jp>
Reply-To: karl@haddock.ima.isc.com (Karl Heuer)
Organization: Interactive Systems, Cambridge, MA 02138-5302
Lines: 23