1

I know in general, a struct instance will have the alignment of its widest scalar member. I declared a structure having a member of long double data type.

struct try
{
    char a;
    long double b;
};
struct try obj;

When i tried to check sizeof(obj) it is coming out as 16. My compiler assumes long double as 12 bytes. So i am not able to understand how exactly padding is being done here and how alignment is happening in structure. I assumed that alignment will be done on basis of long double as it is the widest scalar member. So there should be a 11 byte padding for char and the size of structure variable should come out as 24 but output is 16. So exactly what is happening here ?. I am working on a 64 bit processor.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
LocalHost
  • 910
  • 3
  • 8
  • 24

1 Answers1

4

First of all, your compiler is creating 32-bit output even though you have a 64-bit processor. Anyway, you're assuming that things need to be aligned to a boundary identical to their size, which isn't true in general. In particular, in your case, long doubles take up 12 bytes, but only need to be aligned to a 4-byte boundary. As such, after your single-byte char, the compiler inserts 3 bytes of padding to get to a 4-byte boundary, and then inserts your 12-byte long double. 1+3+12 is 16, so struct try is 16 bytes long.

  • I am following this article for padding and align. : http://www.catb.org/esr/structure-packing/#_structure_alignment_and_padding . Whenever i use a datatype which is less than or equal to 8 bytes in size this rule is working properly(all structure elements are getting aligned to the widest scalar member). but you said that long double is needed to be aligned to 4 byte boundary only. what is reason behind this ? I have'nt read any such rule in the article above. Can you please explain more deeply how actually boundary alignments take place especially when data type is more than 8 byte of size. – LocalHost Apr 25 '20 at 18:18
  • That doesn't contradict what I wrote. There are two scalar members in your struct: `char`, with an alignment of 1, and `long double`, with an alignment of 4. The widest of 1 and 4 is 4, and your structure indeed has alignment 4. – Joseph Sible-Reinstate Monica Apr 25 '20 at 18:20
  • but as i said `long double` is of 12 bytes in my compiler. That's what my whole question is about. Why you are using 4 bytes for alignment. There gotta be some logic behind that ? sorry if i am missing something ? – LocalHost Apr 25 '20 at 18:32
  • @Noshiii That's just what the i386 ABI says. In your compiler, see what `_Alignof(long double)` is. It'll be 4. – Joseph Sible-Reinstate Monica Apr 25 '20 at 18:33
  • Yes it's 4. I got it. thanks a lot. Just one last thing, Alignment of structure depends on `_alignof(type)` and not on `sizeof()` ? Am i right ?? – LocalHost Apr 25 '20 at 18:43
  • 1
    Yes, you're right. Note, though, that it's either `alignof` (from `stdalign.h`) or `_Alignof` (the built-in). `_alignof` doesn't exist. – Joseph Sible-Reinstate Monica Apr 25 '20 at 18:45
  • oh okay, my bad, might be a typo. Will remember in future. thanks a lot for help. – LocalHost Apr 25 '20 at 18:47