- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
有两个相关的C标准规则:
C99 标准,6.3.2.3
:
A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
和7.20.1.4
:
The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:
uintptr_t
这意味着,以下代码是合规的:
int *p = NULL;
void *q = (void*)p;
uintptr_t s = (uintptr_t)q;
但它真的需要两步转换吗?如果执行以下操作,编译器是否会执行隐式中间转换:
int *p = NULL;
uintptr_t s = (uintptr_t)p;
(好吧,它可能适用于大多数编译器,但我的问题是关于标准合规性)
最佳答案
我不会冒险。该标准非常清楚什么是允许的,什么是不允许的。
编写 uintptr_t s = (uintptr_t)(void*)p;
向您代码的读者发出您知道自己在做什么的信号。
关于将非 -`void` 指针转换为 `uintptr_t`,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291377/
这个问题在这里已经有了答案: Can we subtract NULL pointers? (4 个回答) 关闭 2 个月前。 是否定义了NULL - NULL? (char *)NULL - (ch
int y = 1; int *x = &y; printf("%p\n",x); // instead of printing, get this into variable of type un
假设我有以下代码: Foo *foo = something(); uintptr_t bar = reinterpret_cast(foo); bar代表的是foo的地址还是foo指向的地址?我相信
我正在阅读 What is uintptr_t data type但我仍然无法理解 uintptr_t here as 首先将 char array 类型临时转换为 unsigned long int
什么是uintptr_t,它可以用来做什么? 最佳答案 首先,在提出问题时,uintptr_t不在 C++ 中。它在 C99 中,在 中, 作为可选类型。许多 C++03 编译器确实提供了该文件。它
我知道 NULL 不需要是全位零模式。但我想知道如果 uintptr_t x = (uintptr_t)NULL; printf("%" PRIuPTR, x); 保证打印0?我怀疑不是,但只是想确定
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我想在编译时将常量表达式函数指针转换为 std::uintptr_t。我该怎么做? 这是一个最小的例子: #include void fn() {} int main(int argc, char*
这个问题在这里已经有了答案: c++ function addresses coming out different in attached profiler library than in the
它只是 reinterpret_cast 吗? int *pointer; uintptr_t value; value == reinterpret_cast(pointer); 最佳答案 真的取决
C 标准保证 size_t 是可以容纳任何数组索引的类型。这意味着,从逻辑上讲,size_t 应该能够容纳任何指针类型。我在 Google 上发现的一些网站上读到这是合法的和/或应该始终有效: voi
我想检查一些类型 T 的内存对齐。直接的方法是 if (((uintptr_t)&var & __alignof(T) - 1) == 0) ... 但是,uintptr_t 不是现有 C++ 标准的
我需要将任意指针转换为数值,以便通过哈希函数传递它。 理想情况下,只是为了好玩,我还希望数字可以转换回同一个指针。但这不是必须的。 在浏览了 SO 和互联网之后,我不清楚 uintptr_t 或 in
C++(C++11,如果它有所不同)中是否有一个函数可以将字符串转换为 uintptr_t 或 intptr_t?我总是可以使用 atoll() 并在之后转换它,但最好是获得一个函数,该函数对 32
现在我们知道,越界指针运算具有未定义的行为,如 SO question 中所述。 . 我的问题是:我们能否通过转换为 std::uintptr_t 进行算术运算然后转换回指针来解决此类限制?保证有效吗
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: size_t vs. intptr_t 我的一些代码处理指针并将 uintptr_t 作为输入,因为它必须使用
两者都用于存储地址和进行指针运算,两者都在 WinAPI 中定义,我什么时候应该使用 uintptr_t (cstdint) 与 DWORD_PTR (Windows.h )?在x86和x86_64中
阿法克 uintptr_t intptr_t 可用于保存指向 void 的任何指针。因此,这些类型可用于存储指向数据的指针。 在 C99 或更高版本中,是否有类似的有符号和无符号整数类型能够保存指向函
有两个相关的C标准规则: C99 标准,6.3.2.3: A pointer to void may be converted to or from a pointer to any incomple
考虑到我需要将“通用”指针的值存储在结构中并且对指向的内存本身不感兴趣,我发现将其存储为 intptr_t 在此类机器上是否有意义) 关于c - 什么时候 uintptr_t 优于 intptr_t?
我是一名优秀的程序员,十分优秀!