将指针转换为int64_t时,最重要的32位丢失

Bru*_*man 2 c gcc casting

有人可以解释以下奇怪的行为吗?

我在64位Intel平台上运行以下程序:

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

int main(void)
{
  int x;
  int *ptr = &x;

  printf("ptr = %p\n", ptr);
  printf("sizeof(ptr) = %d\n", sizeof(ptr));

  int64_t i1 = (int64_t) ptr;
  printf("i1 = 0x%x\n", i1);
  printf("sizeof(i1) = %d\n", sizeof(i1));

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序产生以下输出:

ptr = 0x7fbfffdf2c
sizeof(ptr) = 8
i1 = 0xbfffdf2c
sizeof(i1) = 8
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么i1只包含最低32位的ptr?(注意它缺少0x7f).

Compiler: gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)
OS: Linux scream 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
Processor: Intel(R) Xeon(R) CPU E5430  @ 2.66GHz
Run Code Online (Sandbox Code Playgroud)

edu*_*ffy 13

它没有去任何地方..你的打印声明是错误的.试试这个:

printf("i1 = 0x%lx\n", i1);
Run Code Online (Sandbox Code Playgroud)