http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54188
Bug #: 54188 Summary: Inconsistent __alignof__(long long) Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: keith.s.thomp...@gmail.com The __alignof__ operator applied to a struct type with a single long long member yields 4. This is inconsistent; a struct's alignment should be at least as large as the alignment of any of its members. I'm using gcc 4.7.0 on Ubuntu 12.04 x86. gcc -v says: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper Target: i686-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.0-7ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --disable-bootstrap --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu Thread model: posix gcc version 4.7.0 (Ubuntu/Linaro 4.7.0-7ubuntu3) Demo program (alignof_bug.c): ======================================== #include <stdio.h> #include <stddef.h> /* * See <http://stackoverflow.com/q/11825081/827263>, * posted by PiotrNycz */ /* * The ALIGNOF macro yields the alignment in bytes of a given type, * determined by how the compiler aligns a member of the type in * a struct following a single char member. */ #define ALIGNOF(type) ((int)(offsetof(struct {char c; type t;}, t))) struct wrapper { long long ll; }; int main(void) { printf("__alignof__(long long) = %d\n", (int)__alignof__(long long)); printf("__alignof__(struct wrapper) = %d\n", (int)__alignof__(struct wrapper)); printf("ALIGNOF(long long) = %d\n", (int)ALIGNOF(long long)); printf("ALIGNOF(struct wrapper) = %d\n", (int)ALIGNOF(struct wrapper)); if (__alignof__(long long) > __alignof__(struct wrapper)) { puts("Inconsistent __alignof__, long long vs. struct"); } if (__alignof__(long long) != ALIGNOF(long long)) { puts("Inconsistent alignment for long long"); } return 0; } ======================================== Output: ======================================== __alignof__(long long) = 8 __alignof__(struct wrapper) = 4 ALIGNOF(long long) = 4 ALIGNOF(struct wrapper) = 4 Inconsistent __alignof__, long long vs. struct Inconsistent alignment for long long ======================================== A response to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10360 suggests that __alignof__ needn't necessarily yield the same result as the offset of a member within a struct. I'm not sure I agree with that, but the main problem I'm reporting here refers to the alignment of the struct type itself, compared to the alignment of one of its members.