warning C4311: 'type cast' : pointer truncation (2024)

  • Forum
  • Visual C++ Programming
  • warning C4311: 'type cast' : pointer truncation

Results 1 to 2 of 2

  • Thread Tools
    • Show Printable Version
    • Email this Page…
  • Display
    • Switch to Hybrid Mode
    • Switch to Threaded Mode
  1. August 10th, 2007,07:15 AM #1

    Patator

    • View Profile
    • View Forum Posts

    warning C4311: 'type cast' : pointer truncation (2)Member warning C4311: 'type cast' : pointer truncation (3)

    Join Date
    Jun 2007
    Posts
    79

    warning C4311: 'type cast' : pointer truncation

    Hi all,

    when i called the function below :

    Code:

    unsigned longChaineInc::hache() const{ if ((!stock) || (!*(stock->c))) return 0; unsigned long graine = 398477; char* car; for (car = stock->c; *car; car++) { graine += *car, graine = (((graine ? graine : 1) * 785398169) % 151147861); } POSTCONDITION(graine >= 0); return graine;}
    from my code :

    Code:

    //const Key gunsigned long i=hache(g) + (unsigned long)this;
    i got this warning:

    warning C4311: 'type cast' : pointer truncation from 'Index_Dico *const ' to 'unsigned long'

    How can i avoid this warning ?

    P.S : i can modify the function hache

    Best regars,

    Reply With Quote

  2. August 10th, 2007,07:51 AM #2

    ashukasama

    • View Profile
    • View Forum Posts

    warning C4311: 'type cast' : pointer truncation (5)Member warning C4311: 'type cast' : pointer truncation (6)warning C4311: 'type cast' : pointer truncation (7)warning C4311: 'type cast' : pointer truncation (8)warning C4311: 'type cast' : pointer truncation (9)

    Join Date
    May 2007
    Posts
    437

    Re: warning C4311: 'type cast' : pointer truncation

    This problem arises when A 64-bit pointer was truncated to a 32-bit int or 32-bit long.

    This warning is only issued when /Wp64 is used.

    From MSDN


    Error Message
    'variable' : pointer truncation from 'type' to 'type'

    This warning detects 64-bit portability issues. For example, if code is compiled on a 64-bit platform, the value of a pointer (64 bits) will be truncated if it is assigned to an int (32 bits).

    See /Wp64

    Detects 64-bit portability problems on types that are also marked with the __w64 keyword.
    /Wp64

    /Wp64 is off by default in the Visual C++ 32-bit compiler and on by default in the Visual C++ 64-bit compiler.

    Variables of the following types are tested on a 32-bit operating system as if they were being used on a 64-bit operating system:

    • int
    • long
    • pointer

    If you regularly compile your application with a 64-bit compiler, you may want to disable /Wp64 in your 32-bit compilations, as the 64-bit compiler will detect all issues. For more information about targeting a Windows 64-bit operating system, see 64-Bit Programming with Visual C++.

    To set this compiler option in the Visual Studio development environment

    1. Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.
    2. Click the C/C++ folder.
    3. Click the General property page.
    4. Modify the Detect 64-bit Portability Issues property.

    To set this compiler option programmatically

    • use Detect64BitPortabilityProblems.

    Also, Have a look Rules for Using Pointers.

    Rules for Using Pointers

    Porting your code to compile for both 32- and 64-bit Microsoft® Windows® is straightforward. You need only follow a few simple rules about casting pointers, and use the new data types in your code. The rules for pointer manipulation are as follows.

    1. Do not cast pointers to int, long, ULONG, or DWORD. If you must cast a pointer to test some bits, set or clear bits, or otherwise manipulate its contents, use the UINT_PTR or INT_PTR type. These types are integral types that scale to the size of a pointer for both 32- and 64-bit Windows (for example, ULONG for 32-bit Windows and _int64 for 64-bit Windows). For example, assume you are porting the following code:

      ImageBase = (PVOID)((ULONG)ImageBase | 1);

      As a part of the porting process, you would change the code as follows:

      ImageBase = (PVOID)((ULONG_PTR)ImageBase | 1);

      Use UINT_PTR and INT_PTR where appropriate (and if you are uncertain whether they are required, there is no harm in using them just in case). Do not cast your pointers to the types ULONG, LONG, INT, UINT, or DWORD.

      Note that HANDLE is defined as a void*, so typecasting a HANDLE value to a ULONG value to test, set, or clear the low-order 2 bits is an error on 64-bit Windows.

    2. Use the PtrToLong or PtrToUlong function to truncate pointers. If you must truncate a pointer to a 32-bit value, use the PtrToLong or PtrToUlong function (defined in Basetsd.h). These functions disable the pointer truncation warning for the duration of the call.

      Use these functions carefully. After you convert a pointer variable using one of these functions, never use it as a pointer again. These functions truncate the upper 32 bits of an address, which are usually needed to access the memory originally referenced by pointer. Using these functions without careful consideration will result in fragile code.

    3. Be careful using OUT parameters. For example, suppose you have a function defined as follows:

      void func( OUT PULONG *PointerToUlong );

      Do not call this function as follows:

      ULONG ul;

      PULONG lp;

      func((PULONG *)&ul);

      lp = (PULONG)ul;

      Instead, use the following call:

      PULONG lp;

      func(&lp);

      Typecasting &ul to PULONG* prevents a compiler error, but the function will write a 64-bit pointer value into the memory at &ul. This code works on 32-bit Windows, but will cause data corruption on 64-bit Windows—and it will be subtle, hard-to-find corruption. The bottom line: Do not play tricks with the C code—straightforward and simple is better.

    4. Be careful with polymorphic interfaces. Do not create functions that accept DWORD parameters for polymorphic data. If the data can be a pointer or an integral value, use the UINT_PTR or PVOID type.

      For example, do not create a function that accepts an array of exception parameters typed as DWORD values. The array should be an array of DWORD_PTR values. Therefore, the array elements can hold addresses or 32-bit integral values. (The general rule is that if the original type is DWORD and it needs to be pointer width, convert it to a DWORD_PTR value. That is why there are corresponding pointer-precision types.) If you have code that uses DWORD, ULONG, or other 32-bit types in a polymorphic way (that is, you really want the parameter or structure member to hold an address), use UINT_PTR in place of the current type.

    5. Use the new window class functions. If you have window or class private data that contains pointers, your code will need to use the following new functions:
      • GetClassLongPtr
      • GetWindowLongPtr
      • SetClassLongPtr
      • SetWindowLongPtr
      These functions can be used on both 32- and 64-bit Windows, but they are required on 64-bit Windows. Prepare for the transition by using these functions now.

      Additionally, you must access pointers or handles in class private data using the new functions on 64-bit Windows. To aid you in finding these cases, the following indexes are not defined in Winuser.h during a 64-bit compile:

      • GWL_WNDPROC
      • GWL_HINSTANCE
      • GWL_HWDPARENT
      • GWL_USERDATA
      Instead, Winuser.h defines the following new indexes:
      • GWLP_WNDPROC
      • GWLP_HINSTANCE
      • GWLP_HWNDPARENT
      • GWLP_USERDATA
      • GWLP_ID
      For example, the following code does not compile:

      SetWindowLong(hWnd, GWL_WNDPROC, (LONG)MyWndProc);
      It should be changed as follows:

      SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)MyWndProc);
      When setting the cbWndExtra member of the WNDCLASS structure, be sure to reserve enough space for pointers. For example, if you are currently reserving sizeof(DWORD) bytes for a pointer value, reserve sizeof(DWORD_PTR) bytes.

    6. Access all window and class data using FIELD_OFFSET. It is common to access window data using hard-coded offsets. This technique is not portable to 64-bit Windows. To make your code portable, access your window and class data using the FIELD_OFFSET macro. Do not assume that the second pointer has an offset of 4.
    7. The LPARAM, WPARAM, and LRESULT types change size with the platform. When compiling 64-bit code, these types expand to 64 bits, because they typically hold pointers or integral types. Do not mix these values with DWORD, ULONG, UINT, INT, int, or long values. Examine how you use these types and ensure that you do not inadvertently truncate values.
    warning C4311: 'type cast' : pointer truncation (10)
    Last edited by ashukasama; August 10th, 2007 at 07:56 AM.Reason: remove link

    ashuwarning C4311: 'type cast' : pointer truncation (11)
    always use code tag

    Reply With Quote

Quick NavigationVisual C++ ProgrammingTop

  • Site Areas
  • Settings
  • Private Messages
  • Subscriptions
  • Who's Online
  • Search Forums
  • Forums Home
  • Forums
    1. Visual C++ Programming
    2. Visual C++ FAQs
    3. C++ (Non Visual C++ Issues)
    4. C++ and WinAPI
    5. Managed C++ and C++/CLI
    6. Xamarin
    7. Graphics Programming
    8. Multithreading
    9. Network Programming
    10. Driver Development
  • C# Programming
    1. C-Sharp Programming
  • Visual Basic Programming
    1. Visual Basic 6.0 Programming
    2. Visual Basic .NET
    3. VBForums
  • Windows 8 and Later Store Development
    1. Modern Windows Apps (Metro)
  • Other .NET Programming
    1. ASP.NET
    2. .NET Framework
      1. .NET Installation and Configuration Issues
    3. ADO.NET
  • Java Programming
    1. Java Programming
  • Other Programming
    1. AJAX
    2. Scripting - Client Side
    3. Database
    4. Crystal Reports
    5. XML
    6. Wireless/Mobile Development
    7. Assembly
    8. Scripting - Server Side (PHP, Perl, etc.)
    9. SharePoint
    10. Python
      1. Python Articles
  • General Discussion
    1. General Developer Topics
    2. Project Planning, Design, and Management
    3. Testers and Testing
    4. IoT, IoE, and Maker Forum (on VBForums)
    5. General Discussion / Chit Chat
  • CodeGuru Community
    1. Feedback
    2. Articles Suggestions / Requests
    3. Testing Area
    4. Programming Projects
      1. C# Game(s) Project
      2. Game Engine Project
      3. C++ Coding Project
      4. Project: Code War
  • Slow Chat Archives
    1. eCamp Chat: Windows 8 for Developers
    2. Slow Chat: Talk with Microsoft Developer Teams
    3. Slow Chat: Developing Multithreaded Applications
    4. Slow Chat: C++0x
    5. Slow Chat: Visual C++: Yesterday, Today, and Tomorrow
  • Jobs
    1. Open Positions (Jobs)
    2. Looking for Work
  • CodeGuru Technical FAQs
    1. C++ FAQs
    2. STL FAQs
    3. Windows SDK FAQs
    4. Visual C++ FAQs
    5. MFC FAQs
    6. ATL FAQs
    7. .NET Framework (non-language specific) FAQs
    8. C# FAQs
    9. Visual Basic .NET FAQs
    10. Visual Basic FAQs
    11. CodeGuru Individual FAQs
    12. CodeGuru Individual Visual Basic FAQs
  • Retired Forum Areas
    1. Silverlight
    2. Directory Services
    3. General Windows and DNA Programming
    4. Windows OS Issues

«Previous Thread|Next Thread»

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Forum Rules

Click Here to Expand Forum to Full Width


Featured



All times are GMT -5. The time now is 01:05 AM.

Copyright TechnologyAdvice

warning C4311: 'type cast' : pointer truncation (2024)
Top Articles
TS Life Smart Molecule Range - Home Based Business 4U
Elixir Medical’s DynamX Sirolimus-Eluting coronary bioadaptor system receives US FDA Breakthrough Device Designation
Spectrum Gdvr-2007
Using GPT for translation: How to get the best outcomes
Amc Near My Location
Week 2 Defense (DEF) Streamers, Starters & Rankings: 2024 Fantasy Tiers, Rankings
Goodbye Horses: The Many Lives of Q Lazzarus
Steamy Afternoon With Handsome Fernando
Nc Maxpreps
Richard Sambade Obituary
Co Parts Mn
Okatee River Farms
Crazybowie_15 tit*
41 annonces BMW Z3 occasion - ParuVendu.fr
Umn Biology
Garrick Joker'' Hastings Sentenced
Amelia Bissoon Wedding
Lenscrafters Huebner Oaks
Busted Barren County Ky
Apne Tv Co Com
Lake Nockamixon Fishing Report
Wal-Mart 140 Supercenter Products
Paychex Pricing And Fees (2024 Guide)
Wbiw Weather Watchers
Understanding Gestalt Principles: Definition and Examples
Inkwell, pen rests and nib boxes made of pewter, glass and porcelain.
Papa Johns Mear Me
Radical Red Ability Pill
Mcclendon's Near Me
Abga Gestation Calculator
Skepticalpickle Leak
Bj's Tires Near Me
Khatrimmaza
Clearvue Eye Care Nyc
Hattie Bartons Brownie Recipe
Kips Sunshine Kwik Lube
Aveda Caramel Toner Formula
Laff Tv Passport
Lovely Nails Prices (2024) – Salon Rates
Andrew Lee Torres
Worcester County Circuit Court
Wal-Mart 140 Supercenter Products
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Fedex Passport Locations Near Me
Martha's Vineyard – Travel guide at Wikivoyage
Why Are The French So Google Feud Answers
The Complete Uber Eats Delivery Driver Guide:
Bridgeport Police Blotter Today
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Kidcheck Login
Ubg98.Github.io Unblocked
King Fields Mortuary
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6587

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.