共用体
我们都知道共用体内存大小是共用体中元素最大的那个元素的大小,那么数据在内存中到底是怎么分配的呢?
以小端存储为例,下面这个例子一目了然
#include<string>
#include<unordered_map>
#include<algorithm>
using namespace std;
union {
int i;
char x[2];
}a;
int main()
{
a.x[0] = 0;
a.x[1] = 10;//10的二进制为1010,位于四字节的高两位的低位 0000 1010 0000 0000
cout << a.i << endl;
cout << "hello world" << endl;
system("pause");
return 0;
}
输出结果是2560,2560的二进制表示是0000 1010 0000 0000