【发布时间】:2016-10-27 09:19:00
【问题描述】:
我做错了什么,消息框中的值不同。这是在 Windows 上的 32 位应用程序中。我读过的所有内容都说 reinterpret_cast 不应该是必要的,即使我尝试它仍然不起作用。还尝试了更大的数据类型来保存指针,但从我读到的内容来看,在 32 位 int 或 DWORD 上应该没问题。
A* a_ptr;
DWORD a_ptr_address = (DWORD)a_ptr;
A* a_recasted_ptr = (A*)a_ptr_address;
//Display Result
char debugString[20];
snprintf(debugString, 20, "%08x", &a_ptr);
MessageBox(NULL, (const char*)debugString, NULL, NULL);
snprintf(debugString, 20, "%08x", &a_recasted_ptr);
MessageBox(NULL, (const char*)debugString, NULL, NULL);
【问题讨论】:
-
首先,
a_ptr指向垃圾,所以这已经是 UB。 -
你打印指向指针的指针,而不是指针本身。
标签: c++ pointers memory casting