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位)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM