32位64位下各種數據類型大小的對比


1.基本數據類型大小的對比

關於數據類型的大小,總是記不住,這里也算有個記錄,順便看一下32位和64位之間的差別:
我寫了一小段測試代碼:
[cpp]  view plain  copy
 
  1. // C++Test.cpp : 定義控制台應用程序的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9.   
  10. //main  
  11. int _tmain(int argc, _TCHAR* argv[])  
  12. {  
  13.     cout << "sizeof(char):" << sizeof(char) << endl;  
  14.     cout << "sizeof(short):" << sizeof(short) << endl;  
  15.     cout << "sizeof(int):" << sizeof(int) << endl;  
  16.     cout << "sizeof(long):" << sizeof(long) << endl;  
  17.     cout << "sizeof(long long):" << sizeof(long long) << endl;  
  18.     cout << "sizeof(unsigned int):" << sizeof(unsigned int) << endl;  
  19.     cout << "sizeof(float):" << sizeof(float) << endl;  
  20.     cout << "sizeof(double):" << sizeof(double) << endl;  
  21.     void* pointer;  
  22.     cout << "sizeof(pointer):" << sizeof(pointer) << endl;  
  23.   
  24.     system("pause");  
  25.     return 0;  
  26. }  
 
看一下結果:
 WIN32下:
sizeof(char):1
sizeof(short):2
sizeof(int):4
sizeof(long):4
sizeof(long long):8
sizeof(unsigned int):4
sizeof(float):4
sizeof(double):8
sizeof(pointer):4
請按任意鍵繼續. . .
x64下:
sizeof(char):1
sizeof(short):2
sizeof(int):4
sizeof(long):4
sizeof(long long):8
sizeof(unsigned int):4
sizeof(float):4
sizeof(double):8
sizeof(pointer):8
 
32位和64位系統在Windows下基本數據類型的大小都是一樣的。只有指針的大小不一樣!32位指針大小為4byte,而64位的指針大小為8byte。
 
注:Linux下,long型是64位的,這一點是和Windows不同的地方。
 
PS:64位系統下是可以運行32位程序的。但是反過來的話是運行不了的。我在一台32位的機器上運行上面的64位的程序,就會彈出如下提示:

 

 

2.為什么Windowsx64下long也為4byte?

我們知道,正常標准的話,long應該也是64位即8byte。但是在Windows下,我們的結果卻是4byte。為什么會這樣呢?這里引用MSDN的一段關於x64下的解釋:
 
Platform SDK: 64-bit Windows Programming
Abstract Data Models
Every application and every operating system has an abstract data model. Many applications do not explicitly expose this data model, but the model guides the way in which the application's code is written. In the 32-bit programming model (known as the ILP32 model), integer, long, and pointer data types are 32 bits in length. Most developers have used this model without realizing it. For the history of the Win32? API, this has been a valid (although not necessarily safe) assumption to make.

In 64-bit Microsoft? Windows?, this assumption of parity in data type sizes is invalid. Making all data types 64 bits in length would waste space, because most applications do not need the increased size. However, applications do need pointers to 64-bit data, and they need the ability to have 64-bit data types in selected cases. These considerations led to the selection of an abstract data model called LLP64 (or P64). In the LLP64 data model, only pointers expand to 64 bits; all other basic data types (integer and long) remain 32 bits in length.
Initially, most applications that run on 64-bit Windows will have been ported from 32-bit Windows. It is a goal that the same source, carefully written, should run on both 32- and 64-bit Windows. Defining the data model does not make this task easier. However, ensuring that the data model affects only pointer data types is the first step. The second step is to define a set of new data types that allow developers to automatically size their pointer-related data. This allows data associated with pointers to change size as the pointer size changes from 32 bits to 64 bits. Basic data types remain 32 bits in length, so there is no change in the size of data on the disk, data shared over a network, or data shared through memory-mapped files. This relieves developers of much of the effort involved in porting 32-bit code to 64-bit Windows.
These new data types have been added to the Windows API header files. Therefore, you can start using the new types now. For more information, see The New Data Types.
 
簡單解釋一下:
我們編程時很少關注數據類型真正的大小,畢竟即使不關注這個也可以編程,而且我們習慣了Win32,到64位下,只有指針因為尋址需要是必須變成64位的,64位的指針尋址范圍是0~2^64-1,而其他的數據類型基本已經夠用,如果把所有數據類型變成64位,明顯是浪費空間。再者,為了讓32位和64位程序兼容運行,能少修改還是少修改,所以Windows僅將指針大小進行了修改。這樣,程序可以兼容運行。
 

3.指針的大小

我們看看指針到底有多大?指向不同類型對象的指針大小是不是會有不同?看一個小例子:
[cpp]  view plain  copy
 
  1. // C++Test.cpp : 定義控制台應用程序的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9. class Test  
  10. {  
  11.     int num;  
  12.     string name;  
  13. };  
  14. //一個函數指針  
  15. typedef void(*pFunc)(void);  
  16. void PrintHello(void)  
  17. {  
  18.     cout << "hello world" << endl;  
  19. }  
  20. //main  
  21. int _tmain(int argc, _TCHAR* argv[])  
  22. {  
  23.     int* pInt;  
  24.     void* pVoid;  
  25.     Test* pTest = new Test();  
  26.     pFunc pfunc = PrintHello;  
  27.     cout << "sizeof(pInt):" << sizeof(pInt) << endl;  
  28.     cout << "sizeof(pVoid):" << sizeof(pVoid) << endl;  
  29.     cout << "sizeof(pTest):" << sizeof(pTest) << endl;  
  30.     cout << "sizeof(pFunc):" << sizeof(pfunc) << endl;  
  31.   
  32.     system("pause");  
  33.     return 0;  
  34. }  
結果:
Win32下:
sizeof(pInt):4
sizeof(pVoid):4
sizeof(pTest):4
sizeof(pFunc):4
請按任意鍵繼續. . .
x64下:
sizeof(pInt):8
sizeof(pVoid):8
sizeof(pTest):8
sizeof(pFunc):8
請按任意鍵繼續. . .
可見,不管指針指向張三李四還是王二麻子,都是一樣大的。能夠影響指針大小的,還是位數。32位下指針大小為4,64位下指針的大小為8.
 

4.string的大小

關於string的大小,我們寫一小段代碼測試一下:
[cpp]  view plain  copy
 
  1. // C++Test.cpp : 定義控制台應用程序的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include <string>  
  7. using namespace std;  
  8. //main  
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     string empty("");  
  12.     string name("hehe");  
  13.     string longstr("dfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");  
  14.     cout << sizeof(empty) << endl;  
  15.     cout << sizeof(name) << endl;  
  16.     cout << sizeof(longstr) << endl;  
  17.     cout << sizeof(string) << endl;  
  18.     system("pause");  
  19.     return 0;  
  20. }  
結果:
Win32下:
28
28
28
28
請按任意鍵繼續. . .
x64下:
32
32
32
32
請按任意鍵繼續. . .


32位和64位下string差4byte,其實就是一個指針的差別。string內部並不保存字符串本身,而是保存了一個指向字符串開頭的指針。


免責聲明!

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



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