http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52085
Bug #: 52085 Summary: sizeof packed enum can vary without warning depending on include order Classification: Unclassified Product: gcc Version: 4.5.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: joe.eykh...@gmail.com An enum that can be packed to 1 byte will be 4 bytes long if it is used in a function prototype return type (or perhaps a parameter) before its declaration. Suppose the enum is declared in header file enum.h and used in a function prototype in header use.h. If enum.h is included first, the enum will be 1 byte long, otherwise it'll be 4 bytes. It would be very nice if there was a warning about this at least. It would be even better if the size of the enum was treated as indeterminate until the declaration. Otherwise, a structure using this enum would have different sizes in different C files if they include those headers in different orders. #include <stdio.h> enum name func(void); /* comment this out to get 1-byte enum */ enum name { val = 1, } __attribute__ ((__packed__)); enum name x; int main(int argc, char **argv) { printf("%zd\n", sizeof(x)); return 0; } Here's the build and version info: $ gcc -v -save-temps -o t t.c Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i686-redhat-linux/4.5.1/lto-wrapper Target: i686-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch=i686 --build=i686-redhat-linux Thread model: posix gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 't' '-mtune=generic' '-march=i686' /usr/libexec/gcc/i686-redhat-linux/4.5.1/cc1 -E -quiet -v t.c -mtune=generic -march=i686 -fpch-preprocess -o t.i ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.5.1/include-fixed" ignoring nonexistent directory "/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../i686-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/lib/gcc/i686-redhat-linux/4.5.1/include /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 't' '-mtune=generic' '-march=i686' /usr/libexec/gcc/i686-redhat-linux/4.5.1/cc1 -fpreprocessed t.i -quiet -dumpbase t.c -mtune=generic -march=i686 -auxbase t -version -o t.s GNU C (GCC) version 4.5.1 20100924 (Red Hat 4.5.1-4) (i686-redhat-linux) compiled by GNU C version 4.5.1 20100924 (Red Hat 4.5.1-4), GMP version 4.3.1, MPFR version 2.4.2, MPC version 0.8.1 GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128270 GNU C (GCC) version 4.5.1 20100924 (Red Hat 4.5.1-4) (i686-redhat-linux) compiled by GNU C version 4.5.1 20100924 (Red Hat 4.5.1-4), GMP version 4.3.1, MPFR version 2.4.2, MPC version 0.8.1 GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128270 Compiler executable checksum: 96e2ff891a3517bf2aed246a142fe05d COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 't' '-mtune=generic' '-march=i686' as -V -Qy --32 -o t.o t.s GNU assembler version 2.20.51.0.7 (i686-redhat-linux) using BFD version version 2.20.51.0.7-6.fc14 20100318 COMPILER_PATH=/usr/libexec/gcc/i686-redhat-linux/4.5.1/:/usr/libexec/gcc/i686-redhat-linux/4.5.1/:/usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.5.1/:/usr/lib/gcc/i686-redhat-linux/ LIBRARY_PATH=/usr/lib/gcc/i686-redhat-linux/4.5.1/:/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-save-temps' '-o' 't' '-mtune=generic' '-march=i686' /usr/libexec/gcc/i686-redhat-linux/4.5.1/collect2 --build-id --no-add-needed --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o t /usr/lib/gcc/i686-redhat-linux/4.5.1/../../../crt1.o /usr/lib/gcc/i686-redhat-linux/4.5.1/../../../crti.o /usr/lib/gcc/i686-redhat-linux/4.5.1/crtbegin.o -L/usr/lib/gcc/i686-redhat-linux/4.5.1 -L/usr/lib/gcc/i686-redhat-linux/4.5.1/../../.. t.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-redhat-linux/4.5.1/crtend.o /usr/lib/gcc/i686-redhat-linux/4.5.1/../../../crtn.o