TC bug in sizeof()?

Duncan Murdoch dmurdoch at watstat.waterloo.edu
Fri Feb 16 07:25:28 AEST 1990


In article <1519 at maytag.waterloo.edu> I asked:
>
>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?

Thanks to several people who told me that it isn't a bug, but a necessary
property so that relations like sizeof(array) = sizeof(element)*(number of
elements) hold.  

One more question then:  Originally this came up because my friend wanted
to read fixed (odd) sized records from a file, using 

  while (fread(&structure,sizeof(structure),1,data)!=0)
  { ... }

Obviously sizeof() isn't the right thing to use here.  What's the recommended
way to declare that a structure is "packed", and find out its packed size?

Duncan Murdoch



More information about the Comp.lang.c mailing list