http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53505
Bug #: 53505 Summary: bitfield with bool type generated broken object code Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: critical Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: rock...@gmail.com compiler flag: -Wall -Wextra -O[0|1|2|3|fast] All optimization levels: 1,2,3,fast reproduce the bug -O0 is OK. The expected output is 0 //================================================ $g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/opt/libexec/gcc/x86_64-unknown-linux-gnu/4.7.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ../gcc-4.7.0-RC-20120314/configure --prefix=/opt --with-gmp=/opt --with-mpfr=/opt --with-mpc=/opt --with-ppl=/opt --with-cloog=/opt --with-local-prefix=/opt --enable-languages=c,c++ : (reconfigured) ../gcc-4.7.0-RC-20120314/configure --prefix=/opt --with-gmp=/opt --with-mpfr=/opt --with-mpc=/opt --with-ppl=/opt --with-cloog=/opt --with-local-prefix=/opt --enable-languages=c,c++,fortran Thread model: posix gcc version 4.7.0 20120314 (prerelease) (GCC) //================================================ #include <string.h> #include <stdio.h> #include <stdlib.h> #include <vector> #if 1 #define BIT bool // reproduce the bug #else #define BIT unsigned // bug vanished #endif struct A { unsigned int a; unsigned char c1, c2; BIT b1 : 1; BIT b2 : 1; BIT b3 : 1; A() { memset(this, -1, sizeof(A)); a = 0; c1 = c2 = 0; b1 = 0; b2 = 0; b3 = 0; } }; void f(const std::vector<A>& v) { int s = 0; for (int i = 0, n = v.size(); i < n; ++i) { const A a = v[i]; // produce the bug // const A&a = v[i]; // bug vanished if 'a' is a const reference s += a.b1 ? 1 : 0; } printf("%d\n", s); } int main(int argc, char* argv[]) { size_t size = argc < 2 ? 100 : atoi(argv[1]); std::vector<A> v(size); f(v); return 0; }