Bug 12176 - Incorrect size for unions with very large bitfields
Summary: Incorrect size for unions with very large bitfields
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 critical
Target Milestone: 3.4.0
Assignee: Matt Austern
Keywords: ABI, wrong-code
Depends on:
Reported: 2003-09-05 04:52 UTC by Matt Austern
Modified: 2003-12-30 01:10 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2003-12-18 18:01:49


Note You need to log in before you can comment on or make changes to this bug.
Description Matt Austern 2003-09-05 04:52:59 UTC
Consider the following C++ code (it has to be C++, because C imposes limits on bitfield sizes):
extern "C" int printf(const char*,...);

union abcde {
  char foo : 4096;

int main()
  abcde x;
  printf("%d\n", sizeof(x));

When I compile and run this program, I get the result "504".  That makes no sense.  I'm running it 
on a system with 8-bit bytes, so the size of an abcde object should be 512.  Somehow 8 bytes 
have been lost.

Note that this is a problem only with unions, not with structs.

I've seen the same behavior with every version of gcc I've tested, from 2.95 through today's 
mainline, and on both OS X and Linux.
Comment 1 Wolfgang Bangerth 2003-09-05 14:34:16 UTC
Confirmed. That makes it incompatible with icc, which returns 512.

Comment 2 Andrew Pinski 2003-09-15 00:31:31 UTC
Fixed by:
2003-09-14  Mark Mitchell  <mark@codesourcery.com>

	* class.c (layout_class_type): Make DECL_MODE match TYPE_MODE for
	an bit-field whose width exceeds that of its type.
Comment 3 Andrew Pinski 2003-09-15 00:35:29 UTC
That me reopen it because I did not test the patch at all, I should have might have been fixed by.
Comment 4 Andrew Pinski 2003-09-22 08:12:29 UTC
Still happens.
Comment 5 Mark Mitchell 2003-12-30 01:10:46 UTC
This is a problem with -fabi-version=1; it does not occur with -fabi-version=2.

Since changing the size of the union would change the version 1 ABI, and since
this kind of code is very rare, I have closed this PR.