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 位长?


- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01