[Bug target/28763] sizeof macro appears broken when bitfields are in structures

mcvick_e at iname dot com gcc-bugzilla@gcc.gnu.org
Thu Aug 17 22:18:00 GMT 2006



------- Comment #3 from mcvick_e at iname dot com  2006-08-17 22:17 -------

Are you telling me that if I put two of those structures side by side in memory
that GNU will mis-align them even though I pass the flag -mstrict-align?  That
couldn't possibly be since the align flag states to use strict type alignment.

The GNU documentation also states that it will align to the smallest base type
of the bitfield.  Thus an 8-bit bitfield has 8-bit alignment, a 16-bit has
16-bit alignment, and a 32-bit has 32-bit alignment.

In the example I have shown, it clearly shows that a 32-bit bitfield is
defaulting to byte alignment as far as size allocation is concerned.

I've ran objdump on the file, and even though the size is reported as smaller,
it ALLOCATES all 32-bits for alignment reasons.

According to the C-Speficiation (which applies to C++ as well) ...

The size of an object of a structure type is the amount of storage necessary to
represent all components of that type, including any unused padding space
between or after the components. The rule is that the structure will be padded
out to the size the type would occupy as an element of array of such types.

So if you had an array of foo1's and an array of foo2's you would see exactly
the same alignment in memory.  Just that sizeof reports something incorrect. 
Thus if you were wanting to walk the array by say a byte pointer, and then went
to increment that byte pointer by the sizeof the structure, in one case you
would get correct behavior, and in the other you would get undefined or
incorrect behavior.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28763



More information about the Gcc-bugs mailing list