Xref: utzoo comp.sys.ibm.pc:44513 comp.lang.c:25967 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!helios!stat!john From: john@stat.tamu.edu (John S. Price) Newsgroups: comp.sys.ibm.pc,comp.lang.c Subject: Re: TC bug in sizeof()? Message-ID: <4254@helios.TAMU.EDU> Date: 15 Feb 90 23:21:53 GMT References: <1519@maytag.waterloo.edu> Sender: usenet@helios.TAMU.EDU Reply-To: john@stat.tamu.edu (John S. Price) Followup-To: comp.sys.ibm.pc Organization: Texas A&M University Lines: 44 In article <1519@maytag.waterloo.edu> dmurdoch@watstat.waterloo.edu (Duncan Murdoch) writes: > >A friend of mine has found something surprising in TC. Neither of us knows >C well enough to know for sure that this is a bug, but it looks like one. >As illustrated in the program below, if a structure is an odd size, and >is compiled with Word alignment, the sizeof function rounds the size up >one byte. > >Is this a bug? No, it's not a bug. You've basically answered your own question, also. The fact is, the word alignment makes thing WORD ALIGNED. That is, if you define your structure >struct test > { char a; > char b; > char c; > } structure; with word alignment, the sizeof() this IS 4. A word on a PC is 2 bytes, so all structures must fall on 2 byte boundries. If this structure was 3 bytes long, then then next object in memory would fall on a byte boundry, which is wrong. Think of this: struct test array[10]; 10 consecutive test structures in memory. If they weren't 4 bytes long, this array would not work, for you want word alignment. 1 byte 1 byte 1 byte 1 byte 1 byte 1 byte ^ this point right here is on a byte alignment, which is against the word alignment. In memory this structure would be, if you wanted word alignment: It's not a bug. -------------------------------------------------------------------------- John Price | It infuriates me to be wrong john@stat.tamu.edu | when I know I'm right.... --------------------------------------------------------------------------