Size of struct containing double field

2020-03-04 08:39发布

Firstly, I understand byte padding in structs. But I have still a small test contain a double field in struct and I don't know how to explain this :

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));
}

Through out this test, the answer is : 8 8 and 16.

Why this result make me thinking ?

By second test, we can see size of word on my machine is 4 bytes.

By first test, we can see size of double is 8 bytes.

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

But, I don't know why the result is 16 bytes. (So strange with me)

Please explain it for me, thanks :)

标签: c struct sizeof
5条回答
够拽才男人
2楼-- · 2020-03-04 08:58

The procedure typically used for laying out data in a struct is essentially this:

  • Set Offset = 0.
  • For each member in the struct: Let A be its alignment requirement (e.g., 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 with Offset += -Offset & A-1.) Assign the current value of Offset to be the offset of this member. Add the size of the member to Offset.
  • After processing all members: Let A be the greatest alignment requirement of any member. Add to Offset the number of bytes needed to make it a multiple of A. This final value of Offset is the size of the struct.

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

So, for a struct such as struct { char c; double d; int32_t i; }, on a typical implementation, you have:

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

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

(The algorithm breaks if alignment requirements are not powers of two. That could be fixed by making the last step increase the offset to be a multiple of the least common multiple of all the alignments.)

查看更多
3楼-- · 2020-03-04 09:08

It's sixteen bytes so that if you have an array of datas, the double values can all be aligned on 8-byte boundaries. Aligning data properly in memory can make a big difference in performance. Misaligned data can be slower to operate on, and slower to fetch and store.

查看更多
ゆ 、 Hurt°
4楼-- · 2020-03-04 09:11

Alignment is up to the compiler unless you explicitly specify it using compiler specific directives.

Variables aren't necessarily word aligned. Sometimes they're double word aligned for efficieny. In the particular case of floating points, they can be aligned to even higher values so that SSE will work.

查看更多
虎瘦雄心在
5楼-- · 2020-03-04 09:12

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

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

As in single, you have

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

it's the same in data:

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

which is 16.

查看更多
萌系小妹纸
6楼-- · 2020-03-04 09:17

The compiler probably aligns all structure sizes to be a multiple of 8

查看更多
登录 后发表回答