【发布时间】:2020-08-15 23:50:15
【问题描述】:
我已经读过 sizeof union 给出了 union 中成员的最大大小。但是当我这样做时:我得到了意想不到的结果:
#include <iostream>
using namespace std;
union RecordType
{
int arr[9];
double x;
};
int main()
{
RecordType t;
cout << sizeof(t); //output is 40,instead of 36 (9*4bytes each)
}
为什么大小是 40 而不是 36,即使我将数组更改为“int arr[10]”,然后 sizeof 给出的输出为 40(与“int arr[9]”的情况相同),什么内部发生在这里?
【问题讨论】:
-
原因是内存对齐。