Why casts of type (UINT) (void *) (DWORD) are needed?

This is the default HashKey function in the MFC CMap class.

AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key)
{
  // default identity hash - works for most primitive values
  return ((UINT)(void*)(DWORD)key) >> 4;
}

My question is why are the types (DWORD) and (void *) needed? I think (DWORD) may have some relationship with compatibility issues for 16-bit machines. But I got confused in void * .

+5
source share
3 answers
template<class ARG_KEY>
AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key)
{
    // default identity hash - works for most primitive values
    return (DWORD)(((DWORD_PTR)key)>>4);
}

What this feature looks like today. Your version came from a very old version of MFC, old enough to support 16-bit programs. MFC was first released in 1992, the days of Windows version 3. Version MFC from version 1.0 to 2.5 supported 16-bit targets. The current version of the function is good for 32-bit and 64-bit code.

16- . 16- 32- . , void * cast .

+8

DWORD DWORD.

void* .

, , , , .

+2

, , , DWORD (Double Word), DWORD. (void*) , , 4 ( ).

void * ( 32b 64b), - .

The last casting on (UINT)is to point to an unsigned integer to get the memory address as a number.

+1
source

All Articles