This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/45510] New: Bug with anonymous unions and bit-fields


        I have isolated the problem to a very small test program. 

         There are 2 tests in main, testa builds an anonymous union with
bit-fields in it. testb builds a similar anonymous union but this time with a
named structure in it but otherwise identical members.

         testa gives incorrect results (seems to ignore bit-fields). testb
gives correct results. 

         This is the output of the program.

testa
bf0.data=0
bf0.a=0
bf0.b=0
bf0.data=1
bf0.a=1
bf0.b=1
bf0.data=1
bf0.a=1
bf0.b=1
testb
bf0.data=0
bf0.my.a=0
bf0.my.b=0
bf0.data=1
bf0.my.a=1
bf0.my.b=0
bf0.data=3
bf0.my.a=1
bf0.my.b=1

Environment:
System: Linux lc-sj1-2293 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007
x86_64 x86_64 x86_64 GNU/Linux
Architecture: x86_64


host: x86_64-unknown-linux-gnu
build: x86_64-unknown-linux-gnu
target: x86_64-unknown-linux-gnu
configured with: ./configure
--prefix=/tools/oss/packages/x86_64-rhel4/gcc/4.1.1 --with-gnu-as
--with-as=/tools/oss/packages/x86_64-rhel4/binutils/default/bin/as
--with-gnu-ld
--with-ld=/tools/oss/packages/x86_64-rhel4/binutils/default/bin/ld
--enable-languages=c,c++,objc

How-To-Repeat:

#include <iostream>
using namespace std;

struct bfa {
      union {
            unsigned int a : 1,
               b : 4;
            unsigned int data;
      };
};

struct bfb {
      struct my_struct {
            unsigned int a : 1,
               b : 4;
      };

      union {
            my_struct my;
            unsigned int data;
      };
};

void
testa()
{
   cout << __func__ << endl;

   bfa bf0;
   bf0.data = 0;

   cout << "bf0.data=" << hex << bf0.data << endl;
   cout << "bf0.a=" << bf0.a << endl;
   cout << "bf0.b=" << bf0.b << endl;

   bf0.a = 1;

   cout << "bf0.data=" << hex << bf0.data << endl;
   cout << "bf0.a=" << bf0.a << endl;
   cout << "bf0.b=" << hex << bf0.b << endl;

   bf0.b = 1;

   cout << "bf0.data=" << hex << bf0.data << endl;
   cout << "bf0.a=" << bf0.a << endl;
   cout << "bf0.b=" << hex << bf0.b << endl;
}

void
testb()
{
   cout << __func__ << endl;

   bfb bf0;
   bf0.data = 0;

   cout << "bf0.data=" << hex << bf0.data << endl;
   cout << "bf0.my.a=" << bf0.my.a << endl;
   cout << "bf0.my.b=" << bf0.my.b << endl;

   bf0.my.a = 1;

   cout << "bf0.data=" << hex << bf0.data << endl;
   cout << "bf0.my.a=" << bf0.my.a << endl;
   cout << "bf0.my.b=" << hex << bf0.my.b << endl;

   bf0.my.b = 1;

   cout << "bf0.data=" << hex << bf0.data << endl;
   cout << "bf0.my.a=" << bf0.my.a << endl;
   cout << "bf0.my.b=" << hex << bf0.my.b << endl;
}

int
main(int argc, char** argv)
{
   testa();
   testb();
   return 0;
}


------- Comment #1 from runipg at broadcom dot com  2010-09-02 23:39 -------
Fix:
        As mentioned testb is a work-around for the coding style used in testa.


-- 
           Summary: Bug with anonymous unions and bit-fields
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: runipg at broadcom dot com
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]