Type cast warning from DWORD to 64 bit pointer

前提是你 提交于 2021-02-05 08:07:32

问题


An old 32 bit C++ application (MS Visual Studio) has code lines like this:

m_value = (PUCHAR)someDWORD;

Where PUCHAR is a pointer to an unsigned char.

Now I have changed to 64 bit and I get a (valid) warning about conversion from DWORD to the 64 bit pointer. My unqualified solution to this is to write like this:

m_value = (PUCHAR)(DWARD_PTR)someDWORD;

Is this the correct way to fix this warning (and potential runtime error)?


回答1:


That savage cast to DWORD_PTR will only pad someDWORD with zeros, it won't bring back the upper half of the pointer value that was lost.

You need someDWORD to be a DWORD_PTR (or, in fact, a std::uintptr_t) from the beginning.



来源:https://stackoverflow.com/questions/57990690/type-cast-warning-from-dword-to-64-bit-pointer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!