public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/28763]  New: sizeof macro appears broken when bitfields are in structures
@ 2006-08-17 17:44 mcvick_e at iname dot com
  2006-08-17 18:02 ` [Bug c++/28763] " pinskia at gcc dot gnu dot org
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: mcvick_e at iname dot com @ 2006-08-17 17:44 UTC (permalink / raw)
  To: gcc-bugs

I have compiled a sample application that demonstrates my problem (see below
for the sample code).

Basically when I have two structures, one that contains a bitfield and one that
does not, the sizeof macro acts differently between the two structures.
These structures are identical as far as content (size allocated), yet sizeof
fails for the structure containing the bitfield.

Accoding to the standards sizeof should report the total number of bytes within
a structure (including pad bytes for alignment).  However it appears as though
when the bitfield mechanism is used, this fails.

Here's a sample code specimen that duplicates the problem...


-------------------- sample app ----------------------------

struct foo1 {
   unsigned int     bar1 : 10;
   unsigned int     bar2 : 16;
   unsigned int     bar3 : 6;
   unsigned short   bar4;
};

struct foo2 {
   unsigned int    bar1;
   unsigned short  bar2;
};

int main(void) {

   static foo1   fubar1 = { 1, 2, 3, 4 };
   static foo2   fubar2 = { 1, 2 };

   static int    sizeofFubar1 = sizeof(foo1);
   static int    sizeofFubar2 = sizeof(foo2);

   return 0;
}

------------------------ end sample app -----------------------

Compile the application with:

c++ -gstabs+ -O2 -mno-bit-align -mcpu=603 -mtune=603 -mstrict-align -nostdlib
-fno-use-cxa-atexit -fcheck-new -fno-rtti -fno-exceptions -fsigned-char -c
sampleApp.cc -o sampleApp.o

if you run objdump -D -C -s -x -z sampleApp.o you will see that:

sizeof(foo1) reports 6
sizeof(foo2) reports 8

even though the two structures are identical in size and alignment
requirements.

We have 6600 occurrances of the sizeof macro in our software, so manually
padding or using the __attribute__((aligned(4))) isn't very tempting to
overcome this problem.

Thanks!


-- 
           Summary: sizeof macro appears broken when bitfields are in
                    structures
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mcvick_e at iname dot com
 GCC build triplet: powerpc-eabi
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: powerpc-unknown-eabi


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


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2009-06-16 20:03 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-17 17:44 [Bug c++/28763] New: sizeof macro appears broken when bitfields are in structures mcvick_e at iname dot com
2006-08-17 18:02 ` [Bug c++/28763] " pinskia at gcc dot gnu dot org
2006-08-17 18:04 ` [Bug target/28763] " pinskia at gcc dot gnu dot org
2006-08-17 22:18 ` mcvick_e at iname dot com
2006-08-17 22:20 ` pinskia at gcc dot gnu dot org
2006-08-17 22:28 ` mcvick_e at iname dot com
2006-08-17 22:35 ` mcvick_e at iname dot com
2006-08-18  0:03 ` mcvick_e at iname dot com
2006-08-22 16:42 ` mcvick_e at iname dot com
2009-06-16 16:25 ` [Bug target/28763] wrong size of struct with some bit-fields on ppc-eabi mcvick_e at iname dot com
2009-06-16 16:27 ` pinskia at gcc dot gnu dot org
2009-06-16 16:30 ` mcvick_e at iname dot com
2009-06-16 16:31 ` pinskia at gcc dot gnu dot org
2009-06-16 16:55 ` mcvick_e at iname dot com
2009-06-16 17:27 ` [Bug target/28763] sizeof() and __attribute__ broken with " joseph at codesourcery dot com
2009-06-16 17:43 ` mcvick_e at iname dot com
2009-06-16 20:03 ` joseph at codesourcery dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).