c - 为什么 size_t when int 足以满足数组的大小?

标签 c int standards size-t

C 标准保证 int 能够存储所有可能的数组大小。至少,这是我从阅读 §6.5.2.1 的第 1 小节(数组下标约束)中了解到的:

One of the expressions shall have type ‘‘pointer to object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.

既然我们要使用int作为数组下标,为什么要用size_t来决定数组的大小呢?

为什么 strlen() 返回 size_tint 就足够了?

最佳答案

术语“整数类型”并不意味着 int - 例如,charshort 是整数类型。

仅仅因为您可以使用 int 来下标数组并不一定意味着它可以到达所有可能的数组元素。

更具体地说,关于 size_tint,一个例子是 int 可能是 16 位类型并且 size_t 可能是 32 位类型(或更常见的 32 位 int 与 64 位 size_t 在当今 64 位平台上的区别)。

关于c - 为什么 size_t when int 足以满足数组的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004415/

相关文章:

c++ - 在 cout 中打印 getline() 字符串时出现奇怪的错误

c++ - 我如何向图书馆工作组提出一项功能?

c++ - multimap<vector<int>> ,char> 怎么排序呢?

php - While循环VS。 $sqlResult -> fetch_all(MYSQLI_ASSOC) ;

c - 使用 MinGW 的 CodeBlocks 中 strcpy 函数的异常行为

c - 排序功能无法正常工作(冒泡排序)-c

c - 反调试技术的强度

c - 双变量输入错误

c++ - C++中整数类型可以存储的最大值

swift - 'Int' 与闭包中的 'UInt8' 不同