-
Pointer truncation
I need some help solving the below warning(s). As seen in the warning I'm trying to cast between a LONG and a class pointer and back again. Printing the sizeof of both types gives the same size (4), so why complain about truncation? Signed bit in LONG?
Code:
WindowBaseFrame.cpp(82) : warning C4311: 'reinterpret_cast' : pointer truncation from 'CWindowBaseFrame *const ' to 'LONG'
WindowBaseFrame.cpp(286) : warning C4312: 'reinterpret_cast' : conversion from 'LONG' to 'CWindowBaseFrame *' of greater size
I'm using VS2003 on a 32-bit machine if that helps!
-
VS2003 warns you about problems that will occur once you try compiling as 64-bit. These problems don't affect your program now, but they will once you want a 64-bit compilation.
My guess is that you're using SetWindowLong and GetWindowLong, right? Use SetWindowLongPtr and GetWindowLongPtr instead. (The offset constant is GWLP_USERDATA now.)
-
Try something like this, adjusting the dereferencing et cetra to match your actual variables. This should work assuming you are attempting assignment.
Code:
LONG smaller = *(LONG*)&bigger;
-
WTF? That's absolutely not what he wants.
-
Your guess is correct, I'm using Set/GetWindowLong. Changing to the Ptr versions didn't remove the warnings though. I googled a bit and found that part of the windows headers contains this:
Code:
#ifdef _WIN64
LONG_PTR SetWindowLongPtr(
HWND hWnd,
int nIndex,
LONG_PTR dwNewLong
);
#else
#define SetWindowLongPtr SetWindowLong
#endif
Guess I can't do much then :/
I'm using extra space in WNDCLASSEX.cbWndExtra, not GWLP_USERDATA.
-
Still ... cast to LONG_PTR instead of LONG and the warning ought to go away. Although I have to admit, I'm not 100% sure of that. There is some ... brokenness there, as I've experienced in the past.
If nothing else helps, you can just disable 4312 and 4311 around the code where you use it.
-
Old post, but if anyone is interested I solved this warning by reinterpret_cast the pointer to a LONG_PTR then static_cast it to a LONG. The other way around I static_cast the LONG to a LONG_PTR then reinterpret_cast it to the pointer. Warnings are gone and it works fine on my 32-bit architechure :).