![](/img/trans.png)
[英]unsigned int vs unsigned long both are 32bit but I can't intermix them without casting… why?
[英]Why is std::size_t 4 bytes on 32bit systems when unsigned long long is 8 bytes on both 32bit and 64 bit systems?
問題很簡單。
在32位系統上:
std::cout << sizeof(unsigned int); //4
std::cout << sizeof(unsigned long long); //8
std::cout << sizeof(std::size_t); //4
在64位系統上:
std::cout << sizeof(unsigned int); //4
std::cout << sizeof(unsigned long long); //8
std::cout << sizeof(std::size_t); //8
我只檢查了MSVC的實現,它看起來像這樣:
#ifdef _WIN64
typedef unsigned __int64 size_t;
#else
typedef unsigned int size_t;
#endif
那么為什么不明確支持32位和64位系統上的std::size_t
unsigned long long
( std::uintmax_t
)呢? 或者我錯了嗎?
size_t
的要點是能夠保持最大可能對象的大小。 在32位系統上,沒有對象可以占用超過2 ** 32個字節,因此32位類型就足夠了。
使用64位類型會浪費空間並且可能在運行時更昂貴。
那將是毫無意義的浪費。 在32位計算機上,您有4 GB的地址空間,因此您不能擁有大於4 GB的對象,因此32位size_t
的范圍是完全足夠的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.