This is the mail archive of the gcc-help@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]

Re: alignment bug?


John Fine wrote:
I don't actually know, but maybe the optimizer figures out that c is never used and eliminates it.

I actually had an "c" stomped on. Looks like a correctness issue to me. Here's what happens if you initialize "c":


> gcc align.c; ./a.out
sizeof(Bar) = 129
sizeof(Foo) = 640
&bar[0]=0x8049180, addr%128=0x0
&bar[1]=0x8049201, addr%128=0x1
&bar[2]=0x8049282, addr%128=0x2
&bar[3]=0x8049303, addr%128=0x3
foo[0].bar[0].c[128] = a
foo[0].bar[1].c[128] = b
foo[0].bar[2].c[128] = c
foo[0].bar[3].c[128] = d

> gcc -O1 align.c; ./a.out
sizeof(Bar) = 129
sizeof(Foo) = 640
&bar[0]=0x8049180, addr%128=0x0
&bar[1]=0x8049200, addr%128=0x0
&bar[2]=0x8049280, addr%128=0x0
&bar[3]=0x8049300, addr%128=0x0
foo[0].bar[0].c[128] = b
foo[0].bar[1].c[128] = c
foo[0].bar[2].c[128] = d
foo[0].bar[3].c[128] =


----------- #include <stdio.h>

#define ALIGNED(n) __attribute__((__aligned__(n)))

typedef struct Bar {
   char c[129];
} Bar ALIGNED(128);

typedef struct Foo {
  Bar bar[4];
} Foo;

Foo foo[4];

main()
{
   int i, j;
   Foo *foop = &foo[0];

   printf("sizeof(Bar) = %d\n", sizeof(Bar));
   printf("sizeof(Foo) = %d\n", sizeof(Foo));

   for (i=0; i < 4; i++) {
      Bar *bar = &foop->bar[i];
      printf("&bar[%d]=%p, addr%128=0x%lx\n",
             i, bar, ((unsigned long)bar) % 128);
      for (j=0; j < 129; j++) {
         bar->c[j] = 'a' + i;
      }
   }

   printf("foo[0].bar[0].c[128] = %c\n", foo[0].bar[0].c[128]);
   printf("foo[0].bar[1].c[128] = %c\n", foo[0].bar[1].c[128]);
   printf("foo[0].bar[2].c[128] = %c\n", foo[0].bar[2].c[128]);
   printf("foo[0].bar[3].c[128] = %c\n", foo[0].bar[3].c[128]);

}


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