What is the use of intptr_t?(intptr_t 有什么用?)
问题描述
我知道它是一个整数类型,可以在不丢失数据的情况下转换为/从指针转换,但我为什么要这样做呢?与 void*
用于保存指针和 THE_REAL_TYPE*
用于指针运算相比,整数类型有什么优势?
I know it is an integer type that can be cast to/from pointer without loss of data, but why would I ever want to do this? What advantage does having an integer type have over void*
for holding the pointer and THE_REAL_TYPE*
for pointer arithmetic?
编辑
标记为已经被问过"的问题不回答这个.问题是使用 intptr_t
作为 void*
的一般替代品是否是一个好主意,并且那里的答案似乎是不要使用 intptr_t",所以我的问题仍然有效:intptr_t
的一个好的用例是什么?
EDIT
The question marked as "already been asked" doesn't answer this. The question there is if using intptr_t
as a general replacement for void*
is a good idea, and the answers there seem to be "don't use intptr_t", so my question is still valid: What would be a good use case for intptr_t
?
推荐答案
主要原因,你不能对 void *
进行按位运算,但你可以对 intptr_t 做同样的操作
.
The primary reason, you cannot do bitwise operation on a void *
, but you can do the same on a intptr_t
.
在许多需要对地址执行按位运算的场合,可以使用intptr_t
.
On many occassion, where you need to perform bitwise operation on an address, you can use intptr_t
.
然而,对于按位运算,最好的方法是使用 unsigned
对应物,uintptr_t
.
However, for bitwise operations, best approach is to use the unsigned
counterpart, uintptr_t
.
如 其他答案中所述,@chux,指针比较是另一个重要方面.
As mentioned in the other answer by @chux, pointer comparison is another important aspect.
此外,FWIW,根据 C11
标准,§7.20.1.4,
Also, FWIW, as per C11
standard, §7.20.1.4,
这些类型是可选的.
这篇关于intptr_t 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:intptr_t 有什么用?


基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01