The warning informs you about a strange sequence of type conversions. A memsize-type is explicitly cast to a 32-bit integer type and then is again cast to a memsize-type either explicitly or implicitly. Such a sequence of conversions leads to a loss of high-order bits. Usually it signals a crucial error.
Consider this sample:
char *p1; char *p2; ptrdiff_t n; ... n = int(p1 - p2);
We have an unnecessary conversion to the 'int' type here. It must not be here and even might cause a failure if p1 and p2 pointers are more than INT_MAX items away from each other in a 64-bit program.
This is the correct code:
char *p1; char *p2; ptrdiff_t n; ... n = p1 - p2;
Let's consider another sample:
BOOL SetItemData(int nItem, DWORD_PTR dwData); ... CItemData *pData = new CItemData; ... CListCtrl::SetItemData(nItem, (DWORD)pData);
This code will cause an error if the CltemData object is created beyond the four low-order Gbytes of memory. This is the correct code:
BOOL SetItemData(int nItem, DWORD_PTR dwData); ... CItemData *pData = new CItemData; ... CListCtrl::SetItemData(nItem, (DWORD_PTR)pData);
One should keep in mind that the analyzer does not generate the warning when conversion is done over such data types as HANDLE, HWND, HCURSOR, and so on. Although these types are in fact pointers (void *), their values always fit into the least significant 32 bits. It is done on purpose so that these handles could be passed between 32-bit and 64-bit processes. For details see How to correctly cast a pointer to int in a 64-bit application?
Have a look at the following example:
typedef void * HANDLE; HANDLE GetHandle(DWORD nStdHandle); int _open_osfhandle(intptr_t _OSFileHandle, int _Flags); .... int fh = _open_osfhandle((int)GetHandle(sh), 0);
We are dealing with a conversion of the following kind:
HANDLE -> int -> intptr_t
That is, the pointer is first cast to the 32-bit 'int' type and then is extended to 'intptr_t'. It doesn't look nice. The programmer should rather have written it like "(intptr_t)GetHandle(STD_OUTPUT_HANDLE)". But there is still no error here as values of the HANDLE type fit into 'int'. That's why the analyzer keeps silent.
If it were written like this:
int fh = _open_osfhandle((unsigned)GetHandle(sh), 0);
the analyzer would generate the message. Mixing signed and unsigned types together spoils it all. Suppose GetHandle() returns INVALID_HANDLE_VALUE. This value is defined in the system headers in the following way:
#define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
Now, what we get after the conversion (intptr_t)(unsigned)((HANDLE)(LONG_PTR)-1) is:
-1 -> 0xffffffffffffffff -> HANDLE -> 0xffffffffu -> 0x00000000fffffffff
The value -1 has turned into 4294967295. The programmer may fail to notice and take this into account and the program will keep running incorrectly if the GetHandle() function returns INVALID_HANDLE_VALUE. Because of that, the analyzer will generate the warning in the second case.