C结构有任何尺寸限制吗?

Nem*_*emo 13 c structure

C结构有任何尺寸限制吗?

Ale*_*nze 12

从C标准:

5.2.4.1翻译限制

1实现应能够翻译和执行至少一个包含以下每个限制的至少一个实例的程序:

... - 对象中的65535个字节(仅在托管环境中)
... - 单个结构或联合中的1023个成员
... - 单个struct-declaration-list中的63个级别的嵌套结构或联合定义13)实施应尽可能避免强加固定的翻译限制.

除此之外,上限是SIZE_MAX(最大值size_t).


Bla*_*iev 6

由于sizeof运算符产生类型的结果size_t,因此限制应该是SIZE_MAX.

您可以SIZE_MAX像这样确定值:

#include <stdint.h>
#include <stdio.h>

int main (void) {
  printf("%zu", SIZE_MAX);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是编译器应该允许的内容.运行时环境允许的是另一个故事.

在实际中在堆栈(本地)上声明一个类似大小的对象将无法工作,因为堆栈可能比它小得多SIZE_MAX.

全局拥有这样的对象可能会使程序启动时抱怨可执行加载程序.

  • @Nemo检查头文件stdint.h,它应该在那里. (2认同)
  • @Nemo:SIZE_MAX依赖于编译器/体系结构,一般来说也意味着依赖于操作系统.在编译器的头文件中查看stdint.h中的定义(或者,如果不存在,可能在limits.h中). (2认同)

Cir*_*四事件 5

实证分析

\n\n

实际上,像 GCC 这样的实现似乎只允许小于 的结构size_t,可能受 约束PTRDIFF_MAX。另请参阅:C 中数组的最大大小是多少?

\n\n

使用:

\n\n
 for i in `seq 32`; do printf "typedef struct { S$i x; S$i y; } S$(($i+1));\\n"; done\n
Run Code Online (Sandbox Code Playgroud)\n\n

我们制定方案:

\n\n
#include <stdint.h>\n#include <stdio.h>\n\ntypedef struct { uint8_t i; } S0;\ntypedef struct { S0 x; S0 y; } S1;\ntypedef struct { S1 x; S1 y; } S2;\ntypedef struct { S2 x; S2 y; } S3;\ntypedef struct { S3 x; S3 y; } S4;\ntypedef struct { S4 x; S4 y; } S5;\ntypedef struct { S5 x; S5 y; } S6;\ntypedef struct { S6 x; S6 y; } S7;\ntypedef struct { S7 x; S7 y; } S8;\ntypedef struct { S8 x; S8 y; } S9;\ntypedef struct { S9 x; S9 y; } S10;\ntypedef struct { S10 x; S10 y; } S11;\ntypedef struct { S11 x; S11 y; } S12;\ntypedef struct { S12 x; S12 y; } S13;\ntypedef struct { S13 x; S13 y; } S14;\ntypedef struct { S14 x; S14 y; } S15;\ntypedef struct { S15 x; S15 y; } S16;\ntypedef struct { S16 x; S16 y; } S17;\ntypedef struct { S17 x; S17 y; } S18;\ntypedef struct { S18 x; S18 y; } S19;\ntypedef struct { S19 x; S19 y; } S20;\ntypedef struct { S20 x; S20 y; } S21;\ntypedef struct { S21 x; S21 y; } S22;\ntypedef struct { S22 x; S22 y; } S23;\ntypedef struct { S23 x; S23 y; } S24;\ntypedef struct { S24 x; S24 y; } S25;\ntypedef struct { S25 x; S25 y; } S26;\ntypedef struct { S26 x; S26 y; } S27;\ntypedef struct { S27 x; S27 y; } S28;\ntypedef struct { S28 x; S28 y; } S29;\ntypedef struct { S29 x; S29 y; } S30;\n/*typedef struct { S30 x; S30 y; } S31;*/\nS30 s;\n\nint main(void) {\n    printf("%jx\\n", (uintmax_t)sizeof(s));\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后在 Ubunbu 17.10 中:

\n\n
$ arm-linux-gnueabi-gcc --version\narm-linux-gnueabi-gcc (Ubuntu/Linaro 7.2.0-6ubuntu1) 7.2.0\nCopyright (C) 2017 Free Software Foundation, Inc.\nThis is free software; see the source for copying conditions.  There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n$ arm-linux-gnueabi-gcc -std=c99 main.c\n
Run Code Online (Sandbox Code Playgroud)\n\n

作品。但如果我们取消注释S31,它就会失败:

\n\n
main.c:35:16: error: type \xe2\x80\x98struct <anonymous>\xe2\x80\x99 is too large\ntypedef struct { S30 x; S30 y; } S31;\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此最大尺寸在 2^30 和 (2^31 - 1) 之间。

\n\n

那么我们可以转换S30为:

\n\n
typedef struct { S29 x; S29 y; uint8_t a[(2lu << 29) - 1]; } S30;\n
Run Code Online (Sandbox Code Playgroud)\n\n

由此我们确定最大尺寸实际上是2^31 - 1 == PTRDIFF_MAX在这个实现上。

\n