This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
g++: __attribute__ ((aligned)) weirdness
- From: Andrew Haley <aph at redhat dot com>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 18 Mar 2003 15:26:46 +0000 (GMT)
- Subject: g++: __attribute__ ((aligned)) weirdness
The doc says
The `aligned' attribute can only increase the alignment; but you
can decrease it by specifying `packed' as well. See below.
but this doesn't seem to be true. For example, on x86 g++ you have a non-POD
class and a subclass:
-----------------------------------------------------------------------------
class Boof
{
int arse ()
{
return 25;
}
void *prickle;
};
class foo : Boof
{
public: long long i;
};
-----------------------------------------------------------------------------
then try:
-----------------------------------------------------------------------------
foo f;
fprintf(stderr, "offset: %d\n", (char *)&f.i - (char *)&f);
-----------------------------------------------------------------------------
offset: 4
Fair enough, this long long is probably 4-aligned.
Now do this:
-----------------------------------------------------------------------------
class bar : Boof
{
public: long long __attribute__((aligned(4))) i;
};
bar b;
fprintf(stderr, "offset: %d\n", (char *)&b.i - (char *)&b);
-----------------------------------------------------------------------------
offset: 8
So you have a field at offset 4. You ask for it to be 4-aligned, and
it jumps to offset 8.
What is going on?
Andrew.