www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/30/12:25:33

From: "Mike Collins" <Mike AT NO_SPAM DOT e-col DOT freeserve DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: A bug in DJGPP? (length of bit fields)
Date: Thu, 30 Dec 1999 13:32:26 -0000
Organization: Customer of Planet Online
Lines: 100
Message-ID: <84fmpj$ptf$1@newsg3.svr.pol.co.uk>
NNTP-Posting-Host: modem-38.fluvoxamine.dialup.pol.co.uk
X-Trace: newsg3.svr.pol.co.uk 946560627 26543 62.136.68.166 (30 Dec 1999 13:30:27 GMT)
NNTP-Posting-Date: 30 Dec 1999 13:30:27 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Good folks of the DJGPP community,

Firstly, I have read the FAQ, and I do not believe the answer to this
question to be there.

I am aware that part-words in structures tend to get padded out to word
boundaries, so, for example, a short int may get padded out to 4 bytes, even
though only two bytes are used for its quantity. I have come across a case,
however, where bit-field variables seem to be being rounded DOWN.

Please look at the short program below, and read the comments on the
structures.
I declared six structures, and wrote a comment line against each of them,
showing what I expected the length of the structure to be.
Then, I compiled and executed the program, and used the results to write a
further comment on each structure to show its actual size as measured by
'sizeof'.
Note that TEST2, TEST3 and TEST6 have actual sizes that are smaller than
expected, and seem to have been rounded DOWN. TEST6 clearly shows two
unsigned integers being put into 4 bytes.

Is this not a bug?

Mike Collins.

#include <stdio.h>

struct TEST1
{ unsigned bit_field_1 : 8,  // expected : 4 bytes (32 bits unsigned)
           bit_field_2 : 8;  // actual : 4
};

struct TEST2
{ short s;
  unsigned bit_field_1 : 8,  // expected : 8 (16 bits short + 16 bits
padding
           bit_field_2 : 8;  //               + 32 bits unsigned)
};                           // actual 4

struct TEST3
{ unsigned bit_field_1 : 8,  // expected : 6 (32 bits unsigned + 16 bits
short
           bit_field_2 : 8;  // actual : 4
  short s;
};

struct TEST4
{ unsigned bit_field_1 : 8,  // expected : 4 (32 bits unsigned)
           bit_field_2 : 8,  // actual : 4
           bit_field_3 : 1;
};

struct TEST5
{ unsigned bit_field_1 : 8,  // expected : 6 (32 bits unsigned + 16 bits
short
           bit_field_2 : 8,  // actual : 8
           bit_field_3 : 1;  //   Why is this different from TEST3 ?
  short s;
};

struct TEST6
{ unsigned bit_field_1 : 8,  // expected : 8 (32 bits unsigned
           bit_field_2 : 8;  //               + 32 bits unsigned)
  unsigned bit_field_3 : 8,  // actual : 4 !!
           bit_field_4 : 8;
};

main()
{ struct TEST1 test1;
  struct TEST2 test2;
  struct TEST3 test3;
  struct TEST4 test4;
  struct TEST5 test5;
  struct TEST6 test6;
  cprintf("\r\nSize of test1 = %d", sizeof(test1));
  cprintf("\r\nSize of test2 = %d", sizeof(test2));
  cprintf("\r\nSize of test3 = %d", sizeof(test3));
  cprintf("\r\nSize of test4 = %d", sizeof(test4));
  cprintf("\r\nSize of test5 = %d", sizeof(test5));
  cprintf("\r\nSize of test6 = %d", sizeof(test6));
 }

Results of running the program :

Size of test1 = 4
Size of test2 = 4
Size of test3 = 4
Size of test4 = 4
Size of test5 = 8
Size of test6 = 4


--
Mike Collins

PS. If replying by e-mail, please make the obvious correction to my address.




- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019