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

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.


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