当最大的成员是整数、浮点类型、_Complex
类型、_Atomic
类型、数据指针或函数指针而不是数组、struct
或union
*时,是否有可能使union
的大小大于最大成员?
例如,是否有(或是否存在)这样的系统,其中以下代码将打印2种不同的大小(假设打印没有失败).如果不是,有没有其他方法可以生成比最大成员更大的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 );
}
它(可能)不会改变任何高效的代码,只是出于好奇.
相关但不相同的问题(使用struct
内部的union
):sizeof union larger than expected. How does type alignment take place here? Size of Union changes with unexpected word alignment
*还有没有其他我忘了的类型?
**这些类型是否有合适的名称?