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
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01