Allocating memory for unions and difference between union pointer and union of pointers

≡放荡痞女 提交于 2021-01-27 14:30:44

问题


Since my question here couldn't be confidently answered, I ask here again in hope that someone knows for sure:

  1. Is there any difference (besides syntactical) between a pointer to a union and a union that contains pointers to its elements? The generated assembly in this example is identical.
  2. As long as I'm never accessing the other members, is it allowed to allocate memory for only one of the members (which isn't the largest)?

Regarding the 2nd question, 6.5.2.1 of the C89 draft says:

The size of a union is sufficient to contain the largest of its members - The value of-at most one of the members can be stored in a union object at any time. A pointer to a union object suitably converted points to each of its members (or if a member is a bit-field. then to the unit in which it resides) and vice versa.

So, at least when properly cast, it should be fine to only allocate space for one of the members, but I couldn't find anything that guarantees that only the corresponding bits of the accessed member are used when using a union.

EDIT:

Given the following definitions:

typedef struct s1
{
    int a;
} s1;

typedef struct s2
{
    int a;
    int b;
} s2;

union u1
{
    s1 a;
    s2 b;
};

Is this legal:

union u1 *u = malloc(sizeof(s1));
u->a.a = 3;
printf("s1.a=%d\n", u->a.a);
printf("s2.a=%d\n", u->b.a);

回答1:


I think you are misunderstanding the pointer-to-union thing.

Using a part of your example code (which you should really have in the question body)

union u1
{
    s1 a;
    s2 b;
};

Then if you have

union u1 my_union;

you are guaranteed that &my_union is equal to e.g. &my_union.a and &my_union.b.


Regarding

union u1 *u = malloc(sizeof(s1));
u->a.a = 3;
printf("s1.a=%d\n", u->a.a);
printf("s2.a=%d\n", u->b.a);

This only works because of two reasons: Type-punning is allowed using unions, and both u->a.a and u->b.a are the exact same size and at the exact same position. The way I see it is that technically it's UB but works because of other requirements.

If you attempted to access u->b.b the UB would be guaranteed.



来源:https://stackoverflow.com/questions/49259777/allocating-memory-for-unions-and-difference-between-union-pointer-and-union-of-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!