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 位长?
基础教程推荐
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
