rfg@lupine.ncd.com (Ron Guilmette) (12/18/90)
I am posting this question to both comp.std.c and comp.std.c++ because the question arises for both languages. Given a machine on which 'long int' is 32 bits and `short int' is 16 bits, what should the following program print? Should this program even be allowed to compile without errors? struct S1 { long field:33; }; struct S2 { short field1:17; short field2:17; int field3; short field4:17; }; extern printf (const char *, ...); struct S1 s1; struct S2 s2; int main () { printf ("sizeof (struct S1) = %d\n", sizeof (struct S1)); printf ("sizeof (struct S2) = %d\n", sizeof (struct S2)); s2.field1 = 0x3FFFF; s2.field2 = 0x3FFFF; s2.field4 = 0x3FFFF; printf ("field1 = %d, field2 = %d, field3 = %d\n", s2.field1, s2.field2, s2.field3); return 0; } As usual, it would be helpful if someone could point me at the appropriate parts of the C standard (or Annotated C++ Reference Manual) where such questions are answered. Thanks.
lijewski@batcomputer.tn.cornell.edu (Mike Lijewski) (12/19/90)
In article <3028@lupine.NCD.COM> rfg@lupine.ncd.com (Ron Guilmette) writes: >I am posting this question to both comp.std.c and comp.std.c++ because >the question arises for both languages. > >Given a machine on which 'long int' is 32 bits and `short int' is 16 bits, >what should the following program print? Should this program even be >allowed to compile without errors? Bitfields are `mostly' implementation defined. ANSI allows bitfields to be of type 'int', 'unsigned int' and 'signed int', though an implementation may choose to accept one of any integer type. An implementation may impose a maximum width on bitfields also. So the best that can be said for the following code is that it is implementation defined. > > >struct S1 { > long field:33; >}; > >struct S2 { > short field1:17; > short field2:17; > int field3; > short field4:17; >}; > >extern printf (const char *, ...); > >struct S1 s1; >struct S2 s2; > >int main () >{ > printf ("sizeof (struct S1) = %d\n", sizeof (struct S1)); > printf ("sizeof (struct S2) = %d\n", sizeof (struct S2)); > > s2.field1 = 0x3FFFF; > s2.field2 = 0x3FFFF; > s2.field4 = 0x3FFFF; > > printf ("field1 = %d, field2 = %d, field3 = %d\n", > s2.field1, s2.field2, s2.field3); > > return 0; >} > >As usual, it would be helpful if someone could point me at the appropriate >parts of the C standard (or Annotated C++ Reference Manual) where such >questions are answered. I'm quoting from the H&S Edition 3, section 5.6.5. > >Thanks. -- Mike Lijewski (H)607/272-0238 (W)607/254-8686 Cornell National Supercomputer Facility ARPA: mjlx@eagle.cnsf.cornell.edu BITNET: mjlx@cornellf.bitnet SMAIL: 25 Renwick Heights Road, Ithaca, NY 14850
diamond@tkou02.enet.dec.com ("diamond@tkovoa") (12/19/90)
Subject: Re: A question on bit-field widths. In article <3028@lupine.NCD.COM> rfg@lupine.ncd.com (Ron Guilmette) writes: >I am posting this question to both comp.std.c and comp.std.c++ because >the question arises for both languages. And the only way I can follow-up is by mailing to comp-std-c@ucbvax.berkeley.edu, sigh. >Given a machine on which 'long int' is 32 bits and `short int' is 16 bits, >what should the following program print? >struct S1 { long field:33; }; >struct S2 { short field1:17; ... }; >... > printf ("sizeof (struct S1) = %d\n", sizeof (struct S1)); > ... Lots of possibilities. I have the impression that a char could be 40 bits, so that sizeof(long int) and sizeof(short int) could both be 1. >Should this program even be allowed to compile without errors? Depending on the implementation, it should be allowed, as above. However, if a char is 16 or 8 or some other expected number of bits, and sizeof(long int) and sizeof(short int) really occupy exactly 32 and 16 bits, then the second Constraint in section 3.5.2.1 is violated and a diagnostic is required. -- Norman Diamond diamond@tkov50.enet.dec.com If this were the company's opinion, I wouldn't be allowed to post it.