2016-08-30 26 views
2

該標準給出了標準無符號類型以下最低比特寬度:標準和禁食整數等價

unsigned char >= 8 
unsigned short >= 16 
unsigned int >= 16 
unsigned long >= 32 
unsigned long long >= 64 

(隱含地,通過指定最小極大值)。

這是否意味着以下等同性?

unsigned char == uint_fast8_t 
unsigned short == uint_fast16_t 
unsigned int == uint_fast16_t 
unsigned long == uint_fast32_t 
unsigned long long == uint_fast64_t 

回答

3

沒有,因爲默認的「原始數據類型」的大小被編譯器挑選爲給定系統的方便的。方便的意思容易與各種原因一起工作:整數範圍,其他整數類型的大小,向後兼容性等。它不一定被選爲最快的可能。

例如,實際上unsigned int在32位系統上的大小爲32,而在64位系統上的大小爲32。但在64位系統上,uint_fast32_t可能是64位。

試想一下,在最常用的尺寸在實踐中unsigned char與:

Data bus unsigned char uint_fast8_t 
8 bit  8 bit   8 bit 
16 bit  8 bit   16 bit 
32 bit  8 bit   32 bit 
64 bit  8 bit   32/64 bit 

之所以如此,是因爲爲了方便我們需要一個字節類型的工作。然而,優化編譯器可能很好地將這個字節放在一個對齊的地址,並將其作爲32位訪問讀取,因此儘管數據類型的實際大小可能會執行優化。

1

我不知道這是一個問題的答案,但(至少在glibc),int_fastx_tuint_fastx_ttypedef ED根據字的大小:

/* Fast types. */ 

/* Signed. */ 
typedef signed char  int_fast8_t; 
#if __WORDSIZE == 64 
typedef long int  int_fast16_t; 
typedef long int  int_fast32_t; 
typedef long int  int_fast64_t; 
#else 
typedef int   int_fast16_t; 
typedef int   int_fast32_t; 
__extension__ 
typedef long long int  int_fast64_t; 
#endif 

/* Unsigned. */ 
typedef unsigned char  uint_fast8_t; 
#if __WORDSIZE == 64 
typedef unsigned long int uint_fast16_t; 
typedef unsigned long int uint_fast32_t; 
typedef unsigned long int uint_fast64_t; 
#else 
typedef unsigned int  uint_fast16_t; 
typedef unsigned int  uint_fast32_t; 
__extension__ 
typedef unsigned long long int uint_fast64_t; 
#endif 
0

基本上你是對的,但不是法理上的。 int意味着系統的「自然」整數類型。實際上,它通常在32位和64位系統上是32位,而在小系統上是16位,因爲64位整數會破壞太多的接口。所以int本質上是fast_32_t。 但是不能保證。像8位和16位這樣的較小類型可能有更快的32位等效。然而在實踐中使用int可能會給你最快的代碼。

相關問題