Why is a char and a bool the same size in c++?(为什么 c++ 中 char 和 bool 的大小相同?)
问题描述
我正在阅读 C++ 编程语言. Stroustrup 在其中指出 sizeof(char) == 1 和 1 <= sizeof(bool)代码>.具体取决于实施.为什么像布尔值这样简单的值会占用与字符相同的空间?
I'm reading The C++ Programming Language. In it Stroustrup states that sizeof(char) == 1 and 1 <= sizeof(bool). The specifics depend on the implementation. Why would such a simple value as a boolean take the same space as a char?
推荐答案
在现代计算机体系结构中,字节是最小的可寻址内存单元.要将多个位打包到一个字节中,需要应用额外的位移操作.在编译器级别,这是内存与速度要求之间的权衡(在高性能软件中,那些额外的位移操作会不必要地增加和减慢应用程序的速度).
In modern computer architectures, a byte is the smallest addressable unit of memory. To pack multiple bits into a byte requires applying extra bit-shift operations. At the compiler level, it's a trade off of memory vs. speed requirements (and in high-performance software, those extra bit-shift operations can add up and slow down the application needlessly).
这篇关于为什么 c++ 中 char 和 bool 的大小相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 c++ 中 char 和 bool 的大小相同?
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
