1
votes

In a previous question, I learned that when a structure that has 8-byte alignment gets embedded into another structure with 4-byte alignment, a padding is needed before the 8-byte aligned structure.

Understood.

At least I thought that I understood.

The VS 2012 docs say:

For structures, unions, and arrays, the alignment-requirement is the largest alignment-requirement of its members.

So, if I have a structure like this:

typedef struct s_inner {
  unsigned long ul1;
  double        dbl1;
  fourth_struct s4;
  unsigned long ul2;
  int           i1;
} t_inner;

I'd expect all members of this structure would be 8-byte aligned since the double is 8-byte aligned.

But my memory dump shows me this:

memdump

t_inner starts from address 1B8:

  • 1B8: the unsigned long is padded because the structure is 8-byte aligned
  • 1C0: The double consumes 8 bytes
  • 1C8: fourth_struct follows, it has 4-byte alignment

Until now, everything is as expected. But now the alignment switches inside t_inner: On address 1E8, I'd expect to find just the unsigned long here, padded with 4 bytes so that the following int is also aligned on 8 bytes. But it seems as if the alignment has now changed since the unsigned long does not carry padding bytes. Instead, the following int is placed with a 4-byte alignment.

Why does the alignment switch inside t_inner? Which rule is applied here?

1
could anyone please explain the downvotes??? - eckes
No clue about the down-votes. I was about to say that that sentence is trying to describe the alignment requirements of the struct, not its individual members. but Jasen described it already. So upticked there. - WhozCraig
This is quite a good article about data alignment... ibm.com/developerworks/library/pa-dalign Perhaps you'll find it useful :) - Jimbo
"When a structure that has 8-byte alignment gets embedded into another structure with 4-byte alignment, a padding is needed before the 8-byte aligned structure". No. Padding may be required before the inner struct or it may not, depending on the alignment requirements of all the outer struct's members up to and including the inner struct. The alignment requirement of the outer struct doesn't directly factor in, nor does the fact that the outer and inner structs have different alignment requirements. - John Bollinger

1 Answers

4
votes

I'd expect all members of this structure would be 8-byte aligned since the double is 8-byte > aligned.

Well, no. Each type has its own alignment inside the struct, and the struct itself has an alignment that is the greatest of the alignments of it's content.

aligned.    typedef struct s_inner {
      unsigned long ul1;        // 4-aligned
      double        dbl1;       // 8-aligned  (need 4 padding before this)
      fourth_struct s4;         // 4 aligned  size 32
      unsigned long ul2;        // 4 aligned  (no padding)
      int           i1;         // 4 aligned  (no padding)
      // no padding needed as struct is a multiple of 8 bytes
    } t_inner; 

The struct itself has align 8 because of the double.