Path: 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 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