1. 程式人生 > >32位64位下各種資料型別大小的對比

32位64位下各種資料型別大小的對比

1.基本資料型別大小的對比

關於資料型別的大小,總是記不住,這裡也算有個記錄,順便看一下32位和64位之間的差別:
我寫了一小段測試程式碼:

// C++Test.cpp : 定義控制檯應用程式的入口點。  

#include <iostream>  
#include <string>  
using namespace std;  
  
  
//main  
int main(int argc, _TCHAR* argv[])  
{  
    cout << "sizeof(char):" << sizeof(char) << endl;
cout << "sizeof(short):" << sizeof(short) << endl; cout << "sizeof(int):" << sizeof(int) << endl; cout << "sizeof(long):" << sizeof(long) << endl; cout << "sizeof(long long):" << sizeof(long long) << endl;
cout << "sizeof(unsigned int):" << sizeof(unsigned int) << endl; cout << "sizeof(float):" << sizeof(float) << endl; cout << "sizeof(double):" << sizeof(double) << endl; void* pointer; cout << "sizeof(pointer):"
<< sizeof(pointer) << endl; system("pause"); return 0; }

看一下結果:
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.指標的大小

// C++Test.cpp : 定義控制檯應用程式的入口點。  
//  
  
#include "stdafx.h"  
#include <iostream>  
#include <string>  
using namespace std;  
  
class Test  
{  
    int num;  
    string name;  
};  
//一個函式指標  
typedef void(*pFunc)(void);  
void PrintHello(void)  
{  
    cout << "hello world" << endl;  
}  
//main  
int main(int argc, _TCHAR* argv[])  
{  
    int* pInt;  
    void* pVoid;  
    Test* pTest = new Test();  
    pFunc pfunc = PrintHello;  
    cout << "sizeof(pInt):" << sizeof(pInt) << endl;  
    cout << "sizeof(pVoid):" << sizeof(pVoid) << endl;  
    cout << "sizeof(pTest):" << sizeof(pTest) << endl;  
    cout << "sizeof(pFunc):" << sizeof(pfunc) << endl;  
  
    system("pause");  
    return 0;  
}  

結果:
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的大小,我們寫一小段程式碼測試一下:

// C++Test.cpp : 定義控制檯應用程式的入口點。  
//  
  
#include "stdafx.h"  
#include <iostream>  
#include <string>  
using namespace std;  
//main  
int main(int argc, _TCHAR* argv[])  
{  
    string empty("");  
    string name("hehe");  
    string longstr("dfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");  
    cout << sizeof(empty) << endl;  
    cout << sizeof(name) << endl;  
    cout << sizeof(longstr) << endl;  
    cout << sizeof(string) << endl;  
    system("pause");  
    return 0;  
}  

結果:
Win32下:

28
28
28
28

x64下:

32
32
32
32

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

相關推薦

3264各種資料型別大小對比

1.基本資料型別大小的對比 關於資料型別的大小,總是記不住,這裡也算有個記錄,順便看一下32位和64位之間的差別: 我寫了一小段測試程式碼: // C++Test.cpp : 定義控制檯應用程式的入口點。 #include <iostream>

163264各種資料型別的長度大小

(1)16位平臺   char         1個位元組8位  short        2個位元組16位  int             2個位元組16位  long         4個位元組32位  指標         2個位元組16位   (2)

各種資料型別在163264系統所佔位元組差異簡介

編寫C、C++程式時需要考慮每種資料型別在記憶體中所佔的記憶體大小,即使同一種資料型別在不同平臺下所佔記憶體大小亦不相同,具體對比如下: 資料型別 16位系統(byte) 32位系統(by

3264各種資料型別長度

一)64位系統和32位有什麼區別?  1、64bit CPU擁有更大的定址能力,最大支援到16GB記憶體,而32bit只支援4G記憶體 2、64位CPU一次可提取64位資料,比32位提高了一倍,理論上效能會提升1倍。但這是建立在64bit作業系統,64bit軟體的基礎

C++那些細節--3264資料型別的區別

Platform SDK: 64-bit Windows Programming Abstract Data Models Every application and every operating system has an abstract data model. Many applications do

c++中3264作業系統基本資料型別位元組大小

int型字長問題: ① C/C++規定int字長和機器字長相同; ② 作業系統字長和機器字長未必一致; ③ 編譯器根據作業系統字長來定義int字長;   由上面三點可知,在一些沒有作業系統的嵌入式計算機系統上,int的長度與處理器字長一致;有操作 系統時,作業系

Object c/swift,java,c/c++在3264各個平臺上基本資料型別 所佔有的位元組數

現在很多app和伺服器互動,雙方收到對方收據,怎麼才能完整解析訊息,是大家都會遇到的問題。現在來看以下他們的位元組長度差異。 iOS 64位編譯器,對應64位系統,包含機型(iphone5s—同時執行32位應用和64位應用,iphone6, iphone6

3264作業系統基本資料型別位元組大小

在一些沒有作業系統的嵌入式計算機系統上,int的長度與處理器字長一致;有操作系統時,作業系統的字長與處理器的字長不一定一致,此時編譯器根據作業系統的字長來定義int字長:" 比如你在64位機器(處理

vs2010在64系統常見資料型別的位元組數

每臺計算機都有一個字長,指明整數和指標資料的標稱大小。對於一個字長為w位的機器而言,虛擬地址的範圍就是0~2^w-1。所以,64位機的指標變數佔64位(8位元組),32位機的指標變數佔4位元組。 但是,整數int型,在32位下佔4位元組,在64位下卻沒有長到8位元組,目的就

3264作業系統基本資料型別位元組大小 2014-03-12 09:44

int型字長問題: ① C/C++規定int字長和機器字長相同; ② 作業系統字長和機器字長未必一致; ③ 編譯器根據作業系統字長來定義int字長;   由上面三點可知,在一些沒有作業系統的嵌入式計算機系統上,int的長度與處理器字長一致;有操作 系統時,作業系統的字長與處

dll檔案3264檢測工具以及Windows資料夾SysWow64的坑

自從作業系統升級到64位以後,就要不斷的需要面對32位、64位的問題。相信有很多人並不是很清楚32位程式與64位程式的區別,以及Program Files (x86),Program Files的區別。同時,對於程式的dll檔案應該放到System32資料夾,還是SysW

3264編譯器各型別大小和位元組對齊

32位編譯器:32位系統下指標佔用4位元組       char:1個位元組       char*(即指標變數): 4個位元組(32位的定址空間是2^32, 即32個bi

Windows檢視library(即.lib檔案)匯出函式或3264編譯等資訊的方法

開發人員都知道,檢視DLL或exe檔案匯出函式、依賴檔案等資訊,使用Depends即可,Depends.exe隨VC6.0平臺釋出。 但是,Depends卻不能想檢視靜態庫.lib檔案的相關資訊,那如果想 1)檢視.lib檔案資訊; 2)沒有安裝VC6.0環境,卻想檢視DL

在C語言中各種資料型別各佔多少

(一)32位平臺:分為有符號型與無符號型。有符號型:short 在記憶體中佔兩個位元組,範圍為-2^15~(2^15-1)int 在記憶體中佔四個位元組,範圍為-2^31~(2^31-1)long在記憶體中佔四個位元組,範圍為-2^31~2^31-1無符號型:最高位不表示符號位unsigned short 在

32 64系統中 各型別位元組數

int型字長問題: ① C/C++規定int字長和機器字長相同; ② 作業系統字長和機器字長未必一致; ③ 編譯器根據作業系統字長來定義int字長; 由上面三點可知,在一些沒有作業系統的嵌入式計算機系統上,int的長度與處理器字長一致;有操作 系統時,

JavaScript 判斷瀏覽器型別3264

JS判斷出版本以及瀏覽器型別 <script type="text/javascript"> var Sys = {}; var ua = navigator.userAgent.toLowerCase();

3264的CentOS 6.0 安裝 Mono 2.10.8 和Jexus 5.0

正常 nginx ng- 錯誤 保存 模塊 永久 www 要點 轉載:http://www.cnblogs.com/shanyou/archive/2012/01/07/2315982.html CentOS是一個基於RHEL的Linux發行版,其目的是為了提供

NAVICAT for 32/64 及破解工具PatchNavicat

點擊 用戶 postgres esql sqlit oracle style for text    Navicat提供多達 7 種語言供客戶選擇,被公認為全球最受歡迎的數據庫前端用戶介面工具。  它可以用來對本機或遠程的 MySQL、SQL Server、SQLite、O

dumpbin判斷windows程序是32還是64(包括DLL)

tro 信息 option hand article tools style 安裝 net http://blog.csdn.net/csfreebird/article/details/10105681 dumpbin /HEADERS gdal18.dll(or xx

paragon ntfs for mac 15 3264 激活碼序列號下載

tro 驅動 get play 簡單 aid ext3 最新 feature Paragon ntfs 15是Mac下管理硬盤驅動器,創建,刪除,格式化,調整大小和移動分區的先驅磁盤分區管理器軟件。如果您想知道如何在一臺PC上安裝兩個操作系統,這個程序的確可以簡單地創建