gpt4 book ai didi

c++ - 用非零值初始化 void 指针的正确(或最安全)方法?

转载 作者:行者123 更新时间:2023-12-01 18:41:58 31 4
gpt4 key购买 nike

以下作品:

void* p = reinterpret_cast<void*>(0x1000);

但看起来“不正确/不安全”,例如。 0x1000 是一个 int ,甚至不是 uintptr_t ,我可以解决这个问题,但是有更好/更安全的转换方法吗?

最佳答案

0x1000 is an int and not even uintptr_t, I could fix this, but is there a better/safer method of casting

0x1000int ,以及 reinterpret_cast<void*>(0x1000)编译器发出符号扩展指令(此处符号为 0)或带有立即操作数的普通寄存器加载指令,以使值与 void* 一样宽。

由于多种原因,编译器不可能知道 0x1000 是否有效。代表一个有效地址,因此它必须遵守并假设它是一个有效地址。

将表示地址的整数转换为 reinterpret_cast 的指针是目前的现有做法。

关于c++ - 用非零值初始化 void 指针的正确(或最安全)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58518017/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com