C++ : why bool is 8 bits long?(C++:为什么 bool 是 8 位长?)
问题描述
在 C++ 中,我想知道为什么 bool 类型是 8 位长(在我的系统上),而只有一位就足以容纳布尔值?
In C++, I'm wondering why the bool type is 8 bits long (on my system), where only one bit is enough to hold the boolean value ?
我以前认为这是出于性能原因,但是在 32 位或 64 位机器上,寄存器为 32 或 64 位宽,性能优势是什么?
I used to believe it was for performance reasons, but then on a 32 bits or 64 bits machine, where registers are 32 or 64 bits wide, what's the performance advantage ?
或者这只是这些历史"原因之一?
Or is it just one of these 'historical' reasons ?
推荐答案
因为每个 C++ 数据类型都必须是可寻址的.
Because every C++ data type must be addressable.
如何创建指向单个位的指针?你不能.但是您可以创建一个指向字节的指针.所以 C++ 中的布尔值通常是字节大小的.(它也可能更大.这取决于实现.主要是它必须是可寻址的,因此任何 C++ 数据类型都不能小于一个字节)
How would you create a pointer to a single bit? You can't. But you can create a pointer to a byte. So a boolean in C++ is typically byte-sized. (It may be larger as well. That's up to the implementation. The main thing is that it must be addressable, so no C++ datatype can be smaller than a byte)
这篇关于C++:为什么 bool 是 8 位长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++:为什么 bool 是 8 位长?
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- CString 到 char* 2021-01-01
