0

I understand by data alignment concept that int's and floats's should be stored at address which is divisible by 4 (the starting byte address) .According to it, the size of the below structure is 12

 typedef struct{
    char A;
    int B;
    float C;
}y;

I have no doubt in understanding the size of above structure Now my doubt is about the size of below structure

typedef struct {
    double A;
    char B;
    char C;
}x;

the size of x is 16. what my doubt is that the two characters used can be allocated in 2 bytes such that a whole structure uses 10 bytes of data and the remaining 2 bytes can be used to allocate to another short int when it was declared right?

But the compiler uses 16 bytes of data and pads the other 6 cells. I can't understand why it is wasting another 6 cells if it can use them for another variables when they are declared?. Can anyone please help me in understanding the above concept?.(I am assuming the size of int ,float and double as 4,4,8 bytes respectively. )

dbush
  • 205,898
  • 23
  • 218
  • 273
  • This topic may be useful: https://stackoverflow.com/questions/4306186/structure-padding-and-packing – Jose Jul 11 '18 at 15:37
  • What if you had an array of these structs? You'd need each of the elements of the array to be aligned. – Christian Gibbons Jul 11 '18 at 15:39
  • 2
    The basic rule is that members still must be aligned correctly when the struct values are stored in an array. With the double requiring alignment to 8, that can only happen when the struct has a size that is a multiple of 8. 16 is the next best match. – Hans Passant Jul 11 '18 at 15:40

1 Answers1

3

In the case of x, it contains a double which (in your case) has size 8. That means that the structure as a whole needs to be a multiple of that size in order for an array of x to be properly aligned.

Since arrays are contiguously allocated, each member of the array comes immediately after the prior one in memory. If x had size 10, then for an array the second element would have the A member at offset 10. For proper alignment, each array member needs to start at a multiple of the size of the largest element. So the structure contains padding at the end to accomplish this.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • @fvu It's not that it requires bigger alignment. It just needs extra padding so that all elements of an array are aligned. – dbush Jul 11 '18 at 15:43
  • Can you please explain "properly aligned"? I see no reason why doubles can't start on non-8 byte boundaries. Address calculation is just `&x+(sizeof(x)*index)+offset(member)` in bytes. – Paul Ogilvie Jul 11 '18 at 15:51
  • @PaulOgilvie While strictly speaking alignment is implementation defined, in general any non-derived type (`char`, `int`, `double`, pointers, etc.) is expected to reside at a memory address that is a multiple of the size of the object. Some CPUs will generate a fault if unaligned memory access is performed. – dbush Jul 11 '18 at 15:56
  • Is that CPU architecture or memory/bus architecture? (As long as the memory places the data on the data pins of the CPU, the CPU shouldn't care.) Anyway, I understand the padding is apparently a function of the architecture for which the compiler generates code. – Paul Ogilvie Jul 11 '18 at 16:02
  • Hmmm...I forget the cache on the CPU chip, which is part of the memory architecture. So the CPU dictates the memory architecture and so dictates the allignment requirements. Upvote for your answer. – Paul Ogilvie Jul 11 '18 at 16:08