C/C++指针技巧(将指针保存为 int,然后翻译回来)

发布于 2024-12-05 02:35:08 字数 610 浏览 0 评论 0原文

正如标题所说,我目前正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

百思不得你姐 2024-12-12 02:35:08

这一行:

Texture texture = *(Texture*)((void*) texture_pointer);

创建一个 Texture 对象作为原始对象的副本。显然,这与旧地址有不同的地址。

您可以这样做:(

Texture &texture = *(Texture*)((void*) texture_pointer);

即创建对旧引用的引用)。

但一般来说,像这样搞乱指针会带来更多麻烦,而不是值得。

This line:

Texture texture = *(Texture*)((void*) texture_pointer);

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:

Texture &texture = *(Texture*)((void*) texture_pointer);

(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.

勿忘初心 2024-12-12 02:35:08

要强制转换为 void 指针或从 void 指针强制转换,请使用 static_cast。要转换为整数或从整数转换,请使用reinterpret_cast

SomeType* p;

// Make sure the type can hold a pointer.
std::uint64_t i = reinterpret_cast<std::uint64_t>(p);

...

SomeType* q = reinterpret_cast<SomeType*>(i); // Guaranteed to yield p back

For casting to and from void pointers, use static_cast. For casting to and from integers, use reinterpret_cast:

SomeType* p;

// Make sure the type can hold a pointer.
std::uint64_t i = reinterpret_cast<std::uint64_t>(p);

...

SomeType* q = reinterpret_cast<SomeType*>(i); // Guaranteed to yield p back
窝囊感情。 2024-12-12 02:35:08

除了其他答案中已经指出的错误之外,这一行是不正确的,并且很可能在 64 位系统(LP64)上失败:

uintptr_t texture_pointer = (int)((void*) &texture);

它应该是:(

uintptr_t texture_pointer = (uintptr_t)&texture;

假设您出于某种原因想要使用 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):

uintptr_t texture_pointer = (int)((void*) &texture);

It should be:

uintptr_t texture_pointer = (uintptr_t)&texture;

(assuming you want to use C-style casts rather than proper C++ casts for some reason).

荭秂 2024-12-12 02:35:08

最初,您获取在堆栈上而不是堆上分配的对象的地址。该行:

Texture texture = *(Texture*)((void*) texture_pointer);

然后将该对象复制到堆栈上分配的另一个对象中。然后你获取第二个地址的地址。

当您处理两个不同的对象时,您有两个不同的地址。

Initially, you take the address of an object allocated on the stack, not on the heap. The line:

Texture texture = *(Texture*)((void*) texture_pointer);

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.

囍孤女 2024-12-12 02:35:08

如果我正确地计算了“*”,则该行将

Texture texture = *(Texture*)((void*) texture_pointer);

创建texture_pointer 指向的任何内容的副本。

所以,texture和*texture_pointer是不同的存储位置,这就是为什么&texture和texture_pointer有不同的值。

If I have counted the '*'s correctly this line

Texture texture = *(Texture*)((void*) texture_pointer);

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.

铃予 2024-12-12 02:35:08
Texture texture = *(Texture*)((void*) texture_pointer);

该行创建一个新对象并将texture_pointer 指向的对象分配给它。这与创建指针并使其指向对象不同。当您将一个对象分配给另一个对象时,您基本上是将旧对象的内容复制到新对象中。那么,这两个打印地址是不同的,这是有道理的——它们是两个不同对象的地址。

Texture texture = *(Texture*)((void*) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文