为什么在毁灭战士 3 源代码中有 assert( sizeof( bool ) == 1)
Why is there assert( sizeof( bool ) == 1 ) in Doom 3 source?
这是断言。在什么合理的情况下它会失败,为什么游戏要检查它?
某些平台将bool
定义为与int
相同的大小。至少旧版本的Mac OS X(可能还有其他RISC BSD端口)是这样的。据推测,代码使用bool
数组并假设效率。《毁灭战士》已经被移植到很多平台上,所以它对这些事情可能非常谨慎。
它必须在运行时完成,因为没有标准的宏指定sizeof(bool)
,并且编译时检查直到C++11才适用于非宏表达式。
我想我已经遇到了你正在寻找的答案。Doom 3 是跨平台的,在 x86 平台上,bool 由大小为 1 的 gcc 定义。另一方面,在gcc(苹果当时使用的编译器)中,在Mac OS X PowerPC上,它默认为4。使用 -mone-byte-bool 将其更改为 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.
相关文章:
- 写入向量<向量<bool>>
- 让bool方法返回其他整数
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 同一对象的"sizeof"的不同答案
- 如何在 std::vector 中找到<bool>哪些索引是真的?
- Arduino-C++ bool 不会从 false 变为 true
- 为什么在这种情况下,bool 类型的输出等于 0?
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- 错误:不能使用"显式"说明符声明 bool'
- 为什么 C++ 11 在 ios 类中添加了运算符 bool
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 从标准::字符串到标准::矢量<bool>的快速转换
- 为什么 bool 和 _Bool 如果它们在内存中占用 1 个字节,它们只能存储 0 或 1
- 通过 mpi 发送 c++ 标准::矢量<bool>
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- sizeof(size_t) 可以小于 sizeof(int) 吗?
- 为什么使用Pool和Bool而不是int8_t或char
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 为什么 sizeof(ar)/ sizeof(ar[0]) 在传递给函数时无法在向量上正常工作?
- 为什么在毁灭战士 3 源代码中有 assert( sizeof( bool ) == 1)