- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我能相信吗sizeof(size_t) <= sizeof(unsigned long int)
总是正确的,根据 C89 标准?
即,如果我使用 unsigned long
,我不会失去值(value)其中 size_t
是预期的,反之亦然。
最佳答案
也许是对 size_t
发生的事情的最好解释C89 中的整数类型是它的基本原理文档。仔细阅读本文档的第 3.4.4 节:
Sizeof and size_t for C89 (rationale)
第三段说:
The type of sizeof, whatever it is, is published (in the library header ) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long.
这意味着,对于C89的关注,一般来说size_t
与预先存在的 unsigned
相同整数类型,在 C89 中意味着 unsigned char, unsigned short, unsigned int, unsigned long
之一.
特别是,类型为 size_t
的每个值在 unsigned long
范围内.
通过阅读标准 C89 的规范,您还可以看到 sizeof(size_t)<=sizeof(long)
.
现在,C99 的情况有点不同。该标准规定:
size_t
是无符号整数类型。 size_t
的类型[...] 的整数转换等级不应大于 signed long int
的整数转换等级6.2.5 标准杆。 8 对于任意两个符号相同但整数转换秩不同的整数类型(见6.3.1.1),整数转换秩较小的类型的取值范围是另一种类型取值的子范围。
由于 signed long int
的整数转换排名与unsigned long int
相同,这意味着 size_t
的值范围包含在 unsigned long int
的值范围内.但是上面列表中的第 3 项为这条规则的异常(exception)情况敞开了大门。
因此,我们只能说,一个实现使 size_t
的值具有很高的意图。保持在 unsigned long int
的范围内.但我们不能完全确定。
如果您想确定,可以执行以下过程来检查您的系统:
<limits.h>
和 <stdint.h>
为了访问有关您的实现的整数类型的信息。 ULONG_MAX
(来自 <limits.h>
)和 SIZE_MAX
(来自 <stdint.h>
)。一个简短的程序是这样的:
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
int main(void) {
printf("Is the range of size_t containd in that of unsigned long?\n\n");
if (SIZE_MAX <= ULONG_MAX)
printf("Yes");
else
printf("No");
return 0;
}
关于我可以相信 sizeof(size_t) <= sizeof(unsigned long int) 总是正确的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329860/
我不知道下面的代码有什么问题,它应该读取数字并将它们的值与位置放在一个成对的 vector 中,然后对它们进行排序并打印出位置。我用 sort 删除了部分 - 我认为问题就在那里,但我再次收到编译错误
我相信当您将两个 unsigned int 值相加时,返回值的数据类型将是 unsigned int。 但是两个 unsigned int 值相加可能会返回一个大于 unsigned int 的值。
我从左移得到的结果我找不到解释。 unsigned char value = 0xff; // 1111 1111 unsigned char = 0x01; // 0000 0001 std::c
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
根据:http://en.wikipedia.org/wiki/C_data_types您可以使用 unsigned short 类型或 unsigned short int 类型。但是它们之间有什么
我正在尝试在 arduino 草图中实现 CRC16。从网上拿到了crc.c文件,想试试看。我创建了其他文件以允许 crc.c 正确运行。这是我的文件。 crc.h: #ifndef CRC_C_ #
我正在尝试实现一个将存储相关索引的列表。但是,我在标题中提到的 for (index_itr = (list_size - numberOfEvents - 1) 处遇到错误。我在做什么错误,以及如何
这不是跨平台代码...所有内容都在同一平台上执行(即字节序是相同的......小字节序)。 我有这个代码: unsigned char array[4] = {'t', 'e', 's', '
我有一个 8 位 unsigned char vector 和一个 16 位 unsigned short vector std::vector eight_bit_array; std::vecto
这个问题在这里已经有了答案: Is subtracting larger unsigned value from smaller in C++ undefined behaviour? (2 个答案
这个问题已经有答案了: Difference between unsigned and unsigned int in C (5 个回答) 已关闭 6 年前。 在 C 语言中,unsigned 之间有
我遇到了一个我不太理解的警告。该警告是通过比较我认为是一个未签名的内容与另一个未签名的内容而生成的。 来源如下: #include #include #include #include int
好吧,这一定很愚蠢。我在移动一些代码时遇到了这个问题,并认为我打错了字或未能正确使用调试器。作为健全性检查,我创建了这个测试用例,但它似乎仍然失败。 unsigned int vtxIdx
我有一个同事不热衷于使用现代 C++ 例如,当我要求他开始使用 r_value 引用时,他不会这样做。当我要求他使用 std::array 而不是 c 数组(char example[8])时,他不会
我有一个无符号字符数组,例如Data[2]。我需要它来与返回 unsigned int 的函数的输出进行比较。 我尝试将 Data[2] 转换为 unsigned int,反之亦然。它没有用。 我想做
我遇到了一个我不太明白的警告。警告是通过将我认为是未签名的与另一个未签名的进行比较而生成的。 这是来源: #include #include #include #include int mai
在下面的程序中,我使用了 unsigned 关键字。 #include int main() { unsigned char i = 'A'; unsigned j
整数值转换为浮点值并再次返回时是否与原始整数值相同? 例如: unsigned x = 42; double y = x; unsigned z = y; 假设编译器没有优化浮点转换,x == z 是
这个问题在这里已经有了答案: Difference between unsigned and unsigned int in C (5 个答案) 关闭 9 年前。 我理解 unsigned 和 un
您好,我遇到了一个关于位运算的小概念问题。请参阅下面的代码,其中我有一个 4 字节的无符号整数。然后我通过将地址分配给无符号字符来访问各个字节。 然后我将最后一个字节的值设置为 1。并对 unsign
我是一名优秀的程序员,十分优秀!