You receive a C4311 compiler message when you cast a 64-bit pointer to a 32-bit variable (329343)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q329343

SYMPTOMS

When you try to cast a 64-bit pointer variable to a 32-bit integer variable or to a 32-bit long variable, you may receive the following C4311 compiler message: warning C4311: 'type cast' : pointer truncation from 'type1' to 'type2'

Note type1 is the data type of the pointer variable, and type2 is the data type of the variable that you are casting from the pointer type.

CAUSE

This behavior only occurs when you try to cast a 64-bit pointer variable to a 32-bit integer variable or to a 32-bit long variable. This message detects 64-bit portability issues. In Microsoft Windows 32-bit operating systems, you can cast a pointer to a 32-bit integer variable or to a 32-bit long variable. However, you cannot do this in Windows 64-bit operating systems. In Windows 64-bit operating systems, the pointer variable is a 64-bit wide pointer variable. If you try to cast a 64-bit pointer to 32-bit variable, information loss may occur.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In Visual Studio .NET 2003, under Project Types, click Visual C++ Projects, and then click Win32 Console Project under Templates. In Visual Studio .NET 2002, under Project Types, click Visual C++ Projects, and then click Win32 Project under Templates.
  4. In the Name box, type MyProject, and then click OK. The Win32 Application Wizard dialog box appears.
  5. In Visual Studio .NET, click Application Settings, and then click Console application.
  6. Click Finish.
  7. In Solution Explorer, expand all folders.
  8. Right-click MyProject.cpp, and then click Open.
  9. Locate the following code in the MyProject.cpp code window:
    int _tmain(int argc, _TCHAR* argv[])
    {
  10. Add the following code after the code that you located in step 9:
    long* m_pl = NULL;
    long m_l = (long)m_pl;
  11. Double-click stdafx.h, and then add the following header file at the beginning:
    #include <windows.h>
  12. On the File menu, click Save All to save all the files.
  13. On the Build menu, click Build Solution to build the application.
Note For portability reasons, use the LONG64 data type instead of the long data type. To use the LONG64 data type, replace the code that is mentioned in step 9 with the following code:
long* m_pl = NULL;
LONG64 m_l64;
m_l64 = reinterpret_cast<LONG64>(m_pl);
With the reinterpret_cast operator, any pointer type can be converted to any other pointer type. Also, any integral type can be converted to any pointer type and vice versa.

REFERENCES

For additional information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MinorLast Reviewed:1/5/2006
Keywords:kbhowto kbCompiler kbDocs kbinfo KB329343 kbAudDeveloper