2014-01-05 37 views

回答

5

某些平臺將bool定義爲與int相同的尺寸。至少舊版本的Mac OS X(以及其他RISC BSD端口)是這樣的。假設代碼使用bool陣列,並假設效率。 Doom已經被移植到了很多平臺上,所以對於這樣的事情可能非常謹慎。

它必須在運行時完成,因爲沒有指定sizeof(bool)的標準宏,編譯時檢查在C++ 11之前不能與非宏表達式一起工作。

+0

謝謝!但爲什麼它依賴於布爾的大小? –

+0

@golergka如果它正在創建非常大的數組,那麼將'int'替換爲'char'會導致內存需求膨脹並且會影響性​​能。更不用說丟掉可能嚴格違背標準的地址算術的可能性。 – Potatoswatter

+0

「某些平臺將bool定義爲int」 - bool是一種內置類型,從不「定義」爲其他類型,但bool的大小可能與int的大小相同。 –

0

我想我遇到了你正在尋找的答案。 Doom 3是跨平臺的,在x86平臺上,bool由gcc定義,大小爲1.在gcc(當時Apple使用的編譯器)中,另一方面,在Mac OS X PowerPC上,默認爲4.使用-mone字節,布爾將其更改爲1

http://linux.die.net/man/1/g++

-mone-byte-bool 
     Override the defaults for "bool" so that "sizeof(bool)==1". By 
     default "sizeof(bool)" is 4 when compiling for Darwin/PowerPC and 1 
     when compiling for Darwin/x86, so this option has no effect on x86. 

     Warning: The -mone-byte-bool switch causes GCC to generate code 
     that is not binary compatible with code generated without that 
     switch. Using this switch may require recompiling all other 
     modules in a program, including system libraries. Use this switch 
     to conform to a non-default data model. 
相關問題