__attribute__ aligned is not completely implemented

Bruno Haible haible@ilog.fr
Tue Apr 11 11:49:00 GMT 2000


Hi,

I'm trying to attach __attribute__ ((aligned (4))) to a type, but it doesn't
work consistently:

  - When that type is part of a struct, it works.

  - When that type is the base type of an array, the alignment is not
    respected. In other words,
       foo array[4]
    has a different memory layout than
       struct { foo f0; foo f1; foo f2; foo f3; }
    I think this is a bug.

  - sizeof also reports a size which is not a multiple of the requested
    alignment.

I think __attribute__ ((aligned (4))) ought to affect the type as such,
including its size, and not only when it appears as a struct member.

Below is a test case; a program which ought to run to completion. On
m68k, where alignof(int)==2, it crashes because sizeof(foo) evaluates to 6.
Could this be added to the gcc test suite?

                  Bruno Haible <haible@clisp.cons.org>


============================================================================
typedef struct {
  int a;
  short b;
} foo __attribute__ ((aligned (4)));

int size = sizeof(foo);

foo array[4] =
  { { 123, 0 }, { 123, 1 }, { 123, 2 }, { 123, 3 } };
struct { foo f0; foo f1; foo f2; foo f3; } str =
  { { 123, 0 }, { 123, 1 }, { 123, 2 }, { 123, 3 } };

int main ()
{
  int i;
  foo* p;

  if ((size % 4) != 0)
    abort();

  for (i = 0, p = array; i < 4; i++, p++)
    if (p->a != 123 || p->b != i)
      abort();

  for (i = 0, p = (foo*)&str; i < 4; i++, p++)
    if (p->a != 123 || p->b != i)
      abort();

  return 0;
}
==========================================================================


More information about the Gcc-bugs mailing list