英文:
Can a union be larger than the the largest member when the largest member is a primitive type?
问题
有没有可能使一个union
的大小大于最大成员的大小,当最大成员是整数、浮点类型、_Complex
类型、_Atomic
类型、数据指针或函数指针,而不是数组、结构体或联合体*?例如,是否可以(或者是否有)一个系统,下面的代码会打印两个不同的大小(假设打印不会失败)。如果不行,是否有其他方法可以生成一个比最大成员更大的union
,即使最大成员是一个"原始类型"**?
#include <complex.h>
#include <stdint.h>
#include <stdio.h>
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )
#define MAX4(a,b,c,d) MAX( MAX( (a), (b) ), MAX( (c), (d) ) )
union Foo
{
uintmax_t a;
long double f;
void (*fp)(void);
void *data;
double complex c;
};
int main(void)
{
size_t l = MAX4( sizeof(void*), sizeof(void(*)(void)), sizeof(long double), sizeof(uintmax_t) );
l = MAX( l, sizeof(double complex) );
printf( "%zu %zu\n", sizeof(union Foo), l );
}
这(可能)不会改变任何生产代码,只是为了好奇。
相关但不同的问题(在union
内部使用struct
):https://stackoverflow.com/questions/8453881/sizeof-union-larger-than-expected-how-does-type-alignment-take-place-here?noredirect=1&lq=1 https://stackoverflow.com/questions/76075501/size-of-union-changes-with-unexpected-word-alignment
*还有其他类型类别我忘了吗?
**这些类型有没有合适的名称?
英文:
Is there a possibility to make the size of a union
larger than the largest member when the largest member is a integer, floating point type, _Complex
type, _Atomic
type, data pointer or function pointer and not a array, struct
nor union
*?
For example, can there be (or is there) a system where the following code will print 2 different sizes (assuming printing doesn't fail). If not, is there a other way to generate a union
that is larger then the largest member even when the largest member is a "primitive type"**?
#include <complex.h>
#include <stdint.h>
#include <stdio.h>
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )
#define MAX4(a,b,c,d) MAX( MAX( (a), (b) ), MAX( (c), (d) ) )
union Foo
{
uintmax_t a;
long double f;
void (*fp)(void);
void *data;
double complex c;
};
int main(void)
{
size_t l = MAX4( sizeof(void*), sizeof(void(*)(void)), sizeof(long double), sizeof(uintmax_t) );
l = MAX( l, sizeof(double complex) );
printf( "%zu %zu\n", sizeof(union Foo), l );
}
It (probably) doesn't change any productive code, it is just for curiosity.
Related but not the same question (use of a struct
inside the union
): https://stackoverflow.com/questions/8453881/sizeof-union-larger-than-expected-how-does-type-alignment-take-place-here?noredirect=1&lq=1 https://stackoverflow.com/questions/76075501/size-of-union-changes-with-unexpected-word-alignment
*Are there any other classes of types i forgot?
**Is there a proper name for this types?
答案1
得分: 4
有,您可以使用_Alignas
:
union Foo {
_Alignas(32) uintmax_t a;
long double f;
void (*fp)(void);
void *data;
double complex c;
};
//
printf("%zu\n", sizeof(union Foo)); // 32
英文:
> is there a other way to generate a union that is larger then the largest member even when the largest member is a "primitive type"?
Yes, you can use _Alignas
:
union Foo {
_Alignas(32) uintmax_t a;
long double f;
void (*fp)(void);
void *data;
double complex c;
};
//
printf("%zu\n", sizeof(union Foo)); // 32
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论