Compiler Warning (level 1) C4311

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

For the latest documentation on Visual Studio 2017, see Compiler Warning (level 1) C4311 on docs.microsoft.com. 'variable' : pointer truncation from 'type' to 'type'

This warning detects 64-bit pointer truncation issues. For example, if code is compiled for a 64-bit architecture, the value of a pointer (64 bits) will be truncated if it is assigned to an int (32 bits). For more information, see Rules for Using Pointers.

For additional information about common causes of warning C4311, see Common Compiler Errors.

The following code example generates C4311 when compiled for a 64-bit target, and then demonstrates how to fix it:

// C4311.cpp  
// compile by using: cl /W1 C4311.cpp  
int main() {  
   void* p = &p;  
   unsigned int i = (unsigned int) p;   // C4311 for 64-bit targets  
   unsigned long long j = (unsigned long long) p;   // OK  
}  
  

Show: