• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C/C++ PVOID casting question.

jasonja

Golden Member
I recently discovered something odd and I'm trying to find some documentation in the C specs or anywhere else for that matter that explains this.

Assume the following is running on a 32bit machine:


VOID myFunction(PVOID pMyPointer);
{

// assume that in this case pMyPointer = 0x87654321 when
// passed into this function.

ULONG64 pMyBigPointer = (ULONG64)pMyPointer;

.......
}

While debugging a problem I learned that in this case the value of pMyBigPointer was not simply 0x87654321 but instead was 0xffffffff87654321. During the cast the PVOID was treated as a signed number and it carried the sign bit out in the 64bit cast.

This seems odd that ANY pointer would be treated as a signed number. I guess for pointer math you could use negative pointers but this seems pretty hairy. I know how to fix this issue but I'm looking for the explaination as to why PVOID is treated differently than other pointers in this case. Some links backing this up would be nice too.

Thanks in advance.
 
This is a wild guess:

msdn

Do a search for POINTER_64.

My guess is that the compiler was trying to out-smart you and make your 32-bit address a POINTER_32. And since you were casting to a 64-bit address, it sign-extended (as the description of POINTER_64 suggests).
 
Back
Top