【发布时间】:2019-12-18 12:17:39
【问题描述】:
struct A
{
union
{
int array[10];
struct
{
int a1;
int a2;
...
...
int a10;
};
};
A a; //1 - structure defination
a.array[0] = 10 //2 - then a.a1 is also assigned with value 10. so how does memory is created.
A *a = new A;
delete a;// does compiler takes care of deleting union and struct members inside struct ?
在网上看到这个sn-p代码,谁能解释一下如何为上述结构创建内存,因为当一个值被分配给数组联合时,它被分配给内部结构变量。
我认为最初最里面的 Struct 会为每个成员分别创建内存,如何为 union.union 创建一个具有 10*double 大小的单个块,但如何在内部联合和结构之间创建映射。
【问题讨论】:
-
"联合体的大小仅与容纳其最大数据成员所需的一样大。其他数据成员分配在与该最大成员的一部分相同的字节中。该分配的详细信息是实现定义... " 来源:en.cppreference.com/w/cpp/language/union
-
“内存已创建”是什么意思?你的意思是“这个结构在内存中是如何布局的?”请尝试使用标准术语。
标签: c++ visual-c++