Re: [問題] long v.s long long int
※ 引述《singlovesong (~"~)》之銘言:
: 開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
: linux gcc
: long 跟 long long int 的差別
: 我用的是linux 64bit
恕刪。
基本上,這應與 cpu \ os 相關性較大,首先知道差別的是,
ILP32, ILP64, LP64, LLP64 為何,下面解釋從字面大概都猜得出來。
(1) ILP32:Int , Long int , Pointer 32 bits
(2) ILP64:Int , Long int , Pointer 64 bits
(3) LP64: , Long int , Pointer 64 bits / Int 32 bits
(4) LLP64: Long Long int , Pointer 64 bits
------------------------------------------------
歸納一個表出來大概長這樣,打星號是我認為該額外注意的地方
資料型態 ILP32 ILP64 LP64 LLP64
char 8 8 8 8
short 16 16 16 16
int 32 64 *32 *32
long 32 64 64 *32
long long 64 64 64 64
*pointer 32 64 64 64
--------------------------------------------------
而在 64 bits cpu 底下,應不存在 ILP32,
(目前沒聽過, ILP32 應只存在 32位元電腦)
注意到上述的 LLP64 和 LP64 ,
雖都是 64 bits cpu ,但其 int 都是 32 bits,
也因這二種模型,所以打破了早期程式設計師常用的技巧
CPU_BIT = sizeof(int)*8;
簡單的說,早期是可以用 int 去判斷 cpu bit 數,
據悉 M$ 首度打破了這規則,它使用的是 LLP64,
很難想像吧!即使在 64 位元 cpu 下,sizeof(long) = sizeof(int) = 4 !!
所以現在用這方式去取 cpu bit 數也不是很準。
----------------------------------------------------
至於你的問題,在我一開始留下的原文那幾行已有跡可尋,
Unix、Linux 是走 LP64,所以你跑出來的結果才會是那樣。
至於你說的「很多網站」,應大多都是以 windows 環境為主,
或以其書為主,於此便不再探討為何有誤。
故,能引入 header,直接使用已事先定義好之 macro 方為上策。
-----------------------------------------------------
上面還有一個星號沒講到,是 pointer 部份,
這部份會特別打星號是因為,若從 32bits 轉到 64bits 下,
很多 bug 都很好抓,最難抓的是 sizeof(pointer) 這部份,
早期 C 語言寫的時候,部份 code 沒考慮到這點,
進而造成 Memory corruption ,在移植時這部份出包卻又非常難抓!
我不確定 stdint.h 或其它 header 有沒有明確指出,
但若考慮有移植性問題時,確實該謹慎考量。
最後再提二點
1. 不是 64 位元電腦,跑程式就以 64 位元變數較快,一律都是跑 int 較快!
所以 windows 底下,在 64 位元 CPU 下,也是跑 32 bits 之 int 較快。
2. 沒記錯的話,規格中只有規定
sizeof(int) <= sizeof(long int) <= sizeof(long long int)
另 sizeof(float) = 4, sizeof(double)=8 , 這二支是固定的,
唯一關係為
sizeof(long double) >= sizeof(double)
至於 long double / double / float 在運算速度上之差別,
於此便不再贅述, 因確實又要花點篇幅說明。
--
YouLoveMe() ? LetItBe() : LetMeFree();
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 180.177.73.222
※ 編輯: tropical72 來自: 180.177.73.222 (06/10 01:49)
→
06/10 01:58, , 1F
06/10 01:58, 1F
討論串 (同標題文章)