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/14191] New: alignment of types is ignored for struct members


Struct members are aligned to 4 bytes only, even though the type of the
struct member is 8.

Environment:
System: Linux linuix 2.4.21-99-default #1 Wed Sep 24 13:30:51 UTC 2003 i686 athlon i386 GNU/Linux
Architecture: i686

host: i586-suse-linux-gnu
build: i586-suse-linux-gnu
target: i586-suse-linux-gnu
configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --enable-languages=c,c++,f77,objc,java,ada --disable-checking --enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit i586-suse-linux

How-To-Repeat:

Look at vector2 below.

================================== bug.c =================================
#include <stddef.h>

typedef unsigned long uintL;
typedef unsigned long long oint;

typedef struct {
  oint u;
} struct1;

typedef struct {
  oint u __attribute__ ((aligned(8)));
} struct2;

int a1 = __alignof (struct1);
int a2 = __alignof (struct2);

typedef struct { uintL l; struct1 o; } vector1;

typedef struct { uintL l; struct2 o; } vector2;

typedef struct { uintL l; struct1 o __attribute__ ((aligned(8))); } vector3;

typedef struct { uintL l; struct2 o __attribute__ ((aligned(8))); } vector4;

int o1 = offsetof (vector1, o);
int o2 = offsetof (vector2, o);
int o3 = offsetof (vector3, o);
int o4 = offsetof (vector4, o);

int main () {
  printf ("%d %d\n", a1, a2);
  printf ("%d %d %d %d\n", o1, o2, o3, o4);
  return 0;
}
=====================================================================

$ gcc bug.c
$ ./a.out
4 8
4 4 8 8

Expected:

$ ./a.out 
4 8
4 8 8 8
------- Additional Comments From bruno at clisp dot org  2004-02-18 12:14 -------
Fix:

A workaround is to label each and every occurrence of struct2 as a struct
member with __attribute__ ((aligned(8))). Very heavy.

-- 
           Summary: alignment of types is ignored for struct members
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bruno at clisp dot org
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i586-suse-linux-gnu
  GCC host triplet: i586-suse-linux-gnu
GCC target triplet: i586-suse-linux-gnu


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


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