C/C++指针技巧(将指针保存为 int,然后翻译回来)
正如标题所说,我目前正在用 C++ 中的指针进行一些小修改,但有些东西不起作用,这就是我得到的:
uintptr_t texture_pointer = (int)((void*) &texture);
其中纹理是一个类;这似乎工作正常,因为我正在获取一个指针值,并且我已确保我在其他函数中获取了相同的值,该函数应该返回对象;这是失败的代码:
std::cout << "C++ BEFORE: " << texture_pointer << std::endl;
Texture texture = *(Texture*)((void*) texture_pointer);
std::cout << "C++ AFTER: " << (uintptr_t)((void*) &texture) << std::endl;
我例外的输出;两个数字是相同的,但是我得到两个不同的数字,因此我认为一定有错误,但我似乎找不到它。
输出示例:
C++ BEFORE: 2685236
C++ AFTER: 2684960
As title says, I'm currently doing some small hacks with pointers in C++, but something isn't working out here's what I got:
uintptr_t texture_pointer = (int)((void*) &texture);
Where texture is a class; this seems to work fine, as I'm getting a pointer value out, and I have insured that I'm getting the same value into my other function, which is supposed to get the object back; this is the code that fails:
std::cout << "C++ BEFORE: " << texture_pointer << std::endl;
Texture texture = *(Texture*)((void*) texture_pointer);
std::cout << "C++ AFTER: " << (uintptr_t)((void*) &texture) << std::endl;
The output I was excepting; was that the same number for both, however I'm getting two different numbers, hence why I think there must be an error, but I can't seem to find it.
Example output:
C++ BEFORE: 2685236
C++ AFTER: 2684960
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这一行:
创建一个新
Texture
对象作为原始对象的副本。显然,这与旧地址有不同的地址。您可以这样做:(
即创建对旧引用的引用)。
但一般来说,像这样搞乱指针会带来更多麻烦,而不是值得。
This line:
creates a new
Texture
object as a copy of the original one. Obviously, this has a different address to the old one.You could do this:
(i.e. create a reference to the old one).
But in general, messing about with pointers like this is more trouble than it's worth.
要强制转换为 void 指针或从 void 指针强制转换,请使用
static_cast
。要转换为整数或从整数转换,请使用reinterpret_cast
:For casting to and from void pointers, use
static_cast
. For casting to and from integers, usereinterpret_cast
:除了其他答案中已经指出的错误之外,这一行是不正确的,并且很可能在 64 位系统(LP64)上失败:
它应该是:(
假设您出于某种原因想要使用 C 风格的转换而不是正确的 C++ 转换)。
As well as the errors pointed out already in other answers, this line is incorrect and may well fail on 64 bit systems (LP64):
It should be:
(assuming you want to use C-style casts rather than proper C++ casts for some reason).
最初,您获取在堆栈上而不是堆上分配的对象的地址。该行:
然后将该对象复制到堆栈上分配的另一个对象中。然后你获取第二个地址的地址。
当您处理两个不同的对象时,您有两个不同的地址。
Initially, you take the address of an object allocated on the stack, not on the heap. The line:
then copies that object into another object allocated on the stack. Then you take the address of the second address.
As you are dealing with two different objects, you have two different addresses.
如果我正确地计算了“*”,则该行将
创建texture_pointer 指向的任何内容的副本。
所以,texture和*texture_pointer是不同的存储位置,这就是为什么&texture和texture_pointer有不同的值。
If I have counted the '*'s correctly this line
creates a copy of whatevery texture_pointer points to.
so, texture and *texture_pointer are different storage locations and that's why &texture and texture_pointer have different values.
该行创建一个新对象并将texture_pointer 指向的对象分配给它。这与创建指针并使其指向对象不同。当您将一个对象分配给另一个对象时,您基本上是将旧对象的内容复制到新对象中。那么,这两个打印地址是不同的,这是有道理的——它们是两个不同对象的地址。
This line creates a new object and assigns the object pointed to by texture_pointer to it. That's different from creating a pointer and making it point to an object. When you assign one object to another, you're basically copying contents of the old one into the new one. It makes sense, then, that the two printed addresses are different -- they're the addresses of two different objects.