Why is 32-bit pointer sign-extended when cast to uint64_t?

牧云@^-^@ 提交于 2019-12-20 04:21:47

问题


When compiled as a 32-bit process, the following code prints ffffffff82223333 instead of 82223333, so it seems like a pointer is always sign-extended when converted to uint64_t. Why is that?

#include <stdint.h>
#include <stdio.h>

int main()
{
    void *p = (void*) 0x82223333;
    uint64_t x = (uint64_t) p;
    printf("%llx\n", x);
}

I thought an address can never be negative, so it should be treated as unsigned.

My question is also related to this question (because Windows handles are just typedefs for pointers).

Similar question, but still no explanation why the compiler developers choose to do this.

来源:https://stackoverflow.com/questions/43035539/why-is-32-bit-pointer-sign-extended-when-cast-to-uint64-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!