Thread: Pointer truncation

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Pointer truncation

    I need some help solving the below warning(s). As seen in the warning I'm trying to cast between a LONG and a class pointer and back again. Printing the sizeof of both types gives the same size (4), so why complain about truncation? Signed bit in LONG?
    Code:
    WindowBaseFrame.cpp(82) : warning C4311: 'reinterpret_cast' : pointer truncation from 'CWindowBaseFrame *const ' to 'LONG'
    WindowBaseFrame.cpp(286) : warning C4312: 'reinterpret_cast' : conversion from 'LONG' to 'CWindowBaseFrame *' of greater size
    I'm using VS2003 on a 32-bit machine if that helps!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    VS2003 warns you about problems that will occur once you try compiling as 64-bit. These problems don't affect your program now, but they will once you want a 64-bit compilation.

    My guess is that you're using SetWindowLong and GetWindowLong, right? Use SetWindowLongPtr and GetWindowLongPtr instead. (The offset constant is GWLP_USERDATA now.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Try something like this, adjusting the dereferencing et cetra to match your actual variables. This should work assuming you are attempting assignment.
    Code:
    LONG smaller = *(LONG*)&bigger;

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    WTF? That's absolutely not what he wants.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Your guess is correct, I'm using Set/GetWindowLong. Changing to the Ptr versions didn't remove the warnings though. I googled a bit and found that part of the windows headers contains this:
    Code:
    #ifdef _WIN64
    LONG_PTR SetWindowLongPtr(
        HWND hWnd,
        int nIndex,
        LONG_PTR dwNewLong
    );
    #else
    #define SetWindowLongPtr SetWindowLong
    #endif
    Guess I can't do much then :/

    I'm using extra space in WNDCLASSEX.cbWndExtra, not GWLP_USERDATA.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Still ... cast to LONG_PTR instead of LONG and the warning ought to go away. Although I have to admit, I'm not 100% sure of that. There is some ... brokenness there, as I've experienced in the past.

    If nothing else helps, you can just disable 4312 and 4311 around the code where you use it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Old post, but if anyone is interested I solved this warning by reinterpret_cast the pointer to a LONG_PTR then static_cast it to a LONG. The other way around I static_cast the LONG to a LONG_PTR then reinterpret_cast it to the pointer. Warnings are gone and it works fine on my 32-bit architechure .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM