The size of the structure containing the double field

Firstly, I understand byte padding in structures. But I still have a small test that contains a double field in the structure, and I donโ€™t know how to explain it:

typedef struct { char a; double b; }data; typedef struct{ char a; int b; }single; int main(){ printf("%d\n",sizeof(double)); printf("%d\n",sizeof(single)); printf("%d\n",sizeof(data)); } 

As a result of this test, the answer is: 8 8 and 16 .

Why does this result make me think?

In the second test, we can see that the word size on my machine is 4 bytes.

In the first test, we see that the size of double is 8 bytes.

So, in struct data : the result should be 12 bytes: 4 bytes for char and 8 bytes for double.

But I do not know why the result is 16 bytes. (So โ€‹โ€‹strange to me)

Please explain it to me, thanks :)

+4
source share
5 answers

The procedure commonly used to lay out data in a structure is essentially this:

  • Set Offset = 0.
  • For each member in the structure: let A be its alignment requirement (for example, 1, 2, 4, or 8 bytes, possibly more). Add to offset the number of bytes needed to make it a multiple of A. (Given that A is a power of two, this can be done using Offset + = -Offset and A-1.) Assign the current value of Offset to the offset of this member. Add penis size to Offset.
  • After processing all the participants: let A be the highest alignment requirement for any member. Add to the offset the number of bytes needed to make it a multiple of A. This final value of Offset is the size of the structure.

As Earnest Friedman-Hill stated, the last step adds padding to the end of the structure, so that in the array of them, each structure begins with the required alignment.

So for a structure like struct { char c; double d; int32_t i; } struct { char c; double d; int32_t i; } struct { char c; double d; int32_t i; } , for a typical implementation, you have:

  • Set the offset to 0.
  • char requires alignment 1, so the offset is already a multiple of 1 (0 * 1 is 0). Put c at this offset, 0. Add size c, 1, to Offset, making it 1.
  • double requires alignment of 8, so add 7 to the offset, making it 8. Place d at this offset, 8. Add size d, 8, to Offset, making it 16.
  • int requires alignment 4, so the offset is already a multiple of 4 (4 * 4 is 16). Place i at this offset 16. Add size i, 4 to Offset, making it 20.
  • In the end, the largest alignment required was 8, so add 4 to the offset, making it 24. Its size is 24 bytes.

Please note that the above has nothing to do with the word size of the machine. It uses only alignment requirements for each member. The alignment requirement may be different for each type, and it may differ from the type size, and the above will still work.

(The algorithm is interrupted if the alignment requirements are not equal to powers of 2. This could be fixed by taking the last step, increasing the offset to be a multiple of the least common multiple of all alignments.)

+4
source

These are sixteen bytes, so if you have an array of data s, double values โ€‹โ€‹can be aligned on 8-byte boundaries. Proper alignment of data in memory can go a long way in performance. Incorrect data may be slower and slower to receive and save.

+8
source

What is strange to you (or am I missing something)?

The logic is the same (filling is done according to the "largest" primitive field in the struct (I mean - int, double, char, etc.))

Like in single , you

 1 (sizeof(char)) + 3 (padding) + 4 (sizeof(int)) 

same thing in data :

 1 (sizeof(char)) + 7 (padding, it sizeof(double) - sizeof(char)) + 8 (sizeof(double)) 

which is 16.

+3
source

The compiler probably aligns all dimensions of the structure as a multiple of 8

+2
source

Alignment depends on the compiler, unless you explicitly specify it using compiler-specific directives.

Variables are not necessarily word aligned. Sometimes they have a double word aligned for effectiveness. In the special case with floating points, they can be aligned with even higher values โ€‹โ€‹for the SSE to work.

+2
source

All Articles