Can a union be larger than the the largest member when the largest member is a primitive type?

huangapple go评论74阅读模式
英文:

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 &lt;complex.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stdio.h&gt;

#define MAX(a,b)       ( (a)&gt;(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( &quot;%zu %zu\n&quot;, 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&amp;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(&quot;%zu\n&quot;, sizeof(union Foo)); // 32

Demo

huangapple
  • 本文由 发表于 2023年7月12日 23:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定