含SIZE_MAX个long元素的数组sizeof评估是否合规?为何直接声明编译失败?
超大
sizeof评估的定义性疑问 考虑以下代码:
#include <stdio.h> #include <limits.h> #include <inttypes.h> #include <stddef.h> int main(){ size_t cnt = SIZE_MAX; size_t sz = sizeof(long[cnt]); printf("%zu\n", sz); }
C标准条款6.5.3.4/p2规定:
若操作数的类型为变长数组类型,则需对操作数进行求值;否则,不对操作数求值,结果为整数常量。
问题核心在于:这种超大的sizeof评估是否具有良好定义?由于size_t是无符号类型,C标准保证无符号整数溢出行为是良好定义的(不同于有符号整数,其溢出可能触发实现定义的信号)。
让我困惑的是,下面这段代码甚至无法编译:
size_t sz = sizeof(long[SIZE_MAX]); //error: size of unnamed array is too large
内容的提问来源于stack exchange,提问作者Some Name
相关产品推荐
相关产品推荐

