linux打印地址时是0xXXXX XXXX XXXX,为什么64位机指针只用48个位?

Example:

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <functional>   //std::function
 6 
 7 using std::cout;
 8 using std::endl;
 9 
10 int func(int *p){
11   *p = 10;
12   return *p;
13 }
14 
15 int func1(int p){
16   // int b =30;
17   p=20;
18   return p;
19 }
20 
21 //c++
22 void swap(int &a,int &b){
23   printf("address in swap(),the address of a and b:%p %p\n",&a,&b);
24   int temp =a;
25   a=b;
26   b=temp;
27 }
28 
29 //c
30 void swap1(int *a,int *b){
31   printf("address in swap1(),the address of a and b:%p %p\n",a,b);
32   // printf("address in main(),the address of a and b:%p %p\n",&a,&b);
33   int temp =*a;
34   printf("address in swap1(),the address of temp:%p\n",temp);
35   // printf("address in swap1(),the address of temp:%d\n",*temp);
36   *a=*b;
37   // printf("address in swap1(),the address of temp:%d\n",*temp);
38   *b=temp;
39 }
40 
41 void swap2(int a,int b){
42   printf("address in swap2(),the address of a and b:%p %p\n",&a,&b);
43   int temp =a;
44   a=b;
45   b=temp;
46 }
47 
48 int main(){
49   int a =5;
50   // int *ptr =&a;
51   // int *ptr=NULL;
52   // a=func(&a);
53   // // cout << "*ptr="<< *ptr << endl;
54   // cout << "a="<< a << endl;
55 
56   // a=func1(a);
57   // cout << "a="<< func1(a) << endl;
58 
59 //swqp
60   int x=4,y=100;
61   printf("address in main(),the address of a and b:%p %p\n",&x,&y);
62   printf("address in main(),the address of a and b:%p %p\n",x,y);
63   cout <<"交换前: "<< "x="<<x<<"y="<<y<<endl;
64   // swap(x,y);
65   swap1(&x,&y);
66   // swap2(x,y);
67   cout << "交换后:"<< "x="<<x<<"y="<<y<<endl;
68   return 0;
69 }
View Code

linux打印出某地址时

那为什么我64位的机器,打印出来的指针却是48位呢?

只用48位的原因很简单:因为现在还用不到完整的64位寻址空间,所以硬件也没必要支持那么多位的地址。

出现这样结果的原因是x86_64处理器硬件限制,为什么要对处理器硬件做限制?因为地址宽度越大,操作系统做虚拟地址转换时越累

接着我在visual studio 2015 进行编译打印,下面给出我x86(32位)、x64(64位)的结果,结果如下:

x86

x64

总结:

linux上的编译器也不尽相同,会出现寻址空间有差异,我的是48位 ;至于window上vs上选不同位数的编译器,有相应的结果。(32位,64位)

 

posted @ 2018-05-31 10:16  Hwangzhiyoung  阅读(2472)  评论(0编辑  收藏  举报