public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* alignment bug?
@ 2008-04-20 12:45 Gagan Arneja
  2008-04-20 13:23 ` Andrew Haley
  2008-04-20 16:19 ` John Fine
  0 siblings, 2 replies; 6+ messages in thread
From: Gagan Arneja @ 2008-04-20 12:45 UTC (permalink / raw)
  To: gcc-help

What am I doing wrong? Why is "bar" getting truncated down to 128?

 > gcc -v
gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)



#include <stdio.h>

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

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

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

Foo foo[4];

main()
{
    int i;
    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);
    }

}

 > ./a.out
sizeof(Bar) = 200
sizeof(Foo) = 896
&bar[0]=0x8049180, addr%128=0x0
&bar[1]=0x8049200, addr%128=0x0
&bar[2]=0x8049280, addr%128=0x0
&bar[3]=0x8049300, addr%128=0x0

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: alignment bug?
  2008-04-20 12:45 alignment bug? Gagan Arneja
@ 2008-04-20 13:23 ` Andrew Haley
  2008-04-20 16:19 ` John Fine
  1 sibling, 0 replies; 6+ messages in thread
From: Andrew Haley @ 2008-04-20 13:23 UTC (permalink / raw)
  To: Gagan Arneja; +Cc: gcc-help

Gagan Arneja wrote:
> What am I doing wrong? Why is "bar" getting truncated down to 128?
>

Looks OK to me.  What is the result that you expect?

> #include <stdio.h>
> 
> #define ALIGNED(n) __attribute__((__aligned__(n)))
> 
> typedef struct Bar {
>    char c[200];
> } Bar ALIGNED(128);
> 
> typedef struct Foo {
>   Bar bar[4];
> } Foo;
> 
> Foo foo[4];
> 
> main()
> {
>    int i;
>    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);
>    }
> 
> }
> 
>> ./a.out
> sizeof(Bar) = 200
> sizeof(Foo) = 896
> &bar[0]=0x8049180, addr%128=0x0
> &bar[1]=0x8049200, addr%128=0x0
> &bar[2]=0x8049280, addr%128=0x0
> &bar[3]=0x8049300, addr%128=0x0
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: alignment bug?
  2008-04-20 12:45 alignment bug? Gagan Arneja
  2008-04-20 13:23 ` Andrew Haley
@ 2008-04-20 16:19 ` John Fine
  2008-04-20 18:30   ` Gagan Arneja
  1 sibling, 1 reply; 6+ messages in thread
From: John Fine @ 2008-04-20 16:19 UTC (permalink / raw)
  To: Gagan Arneja; +Cc: gcc-help

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


Gagan Arneja wrote:

> What am I doing wrong? Why is "bar" getting truncated down to 128?
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: alignment bug?
  2008-04-20 16:19 ` John Fine
@ 2008-04-20 18:30   ` Gagan Arneja
  2008-04-21 12:56     ` Eljay Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: Gagan Arneja @ 2008-04-20 18:30 UTC (permalink / raw)
  To: John Fine; +Cc: gcc-help

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]);

}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: alignment bug?
  2008-04-20 18:30   ` Gagan Arneja
@ 2008-04-21 12:56     ` Eljay Love-Jensen
  2008-04-22  8:47       ` Gagan Arneja
  0 siblings, 1 reply; 6+ messages in thread
From: Eljay Love-Jensen @ 2008-04-21 12:56 UTC (permalink / raw)
  To: Gagan Arneja, John Fine; +Cc: GCC-help

What happens if the ALIGNED is applied to a variable, instead of applying it
to a data type?

For example:

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

--Eljay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: alignment bug?
  2008-04-21 12:56     ` Eljay Love-Jensen
@ 2008-04-22  8:47       ` Gagan Arneja
  0 siblings, 0 replies; 6+ messages in thread
From: Gagan Arneja @ 2008-04-22  8:47 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: John Fine, GCC-help

That works fine.

--
Gagan

Eljay Love-Jensen wrote:
> What happens if the ALIGNED is applied to a variable, instead of applying it
> to a data type?
> 
> For example:
> 
> typedef struct Bar
> {
>   char c[129] ALIGNED(128);
> } Bar;
> 
> --Eljay
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-04-21 15:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-20 12:45 alignment bug? Gagan Arneja
2008-04-20 13:23 ` Andrew Haley
2008-04-20 16:19 ` John Fine
2008-04-20 18:30   ` Gagan Arneja
2008-04-21 12:56     ` Eljay Love-Jensen
2008-04-22  8:47       ` Gagan Arneja

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).