问题
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