为什么char和bool在c ++中的大小相同?(Why is a char and a bool the same size in c++?)

编程入门 行业动态 更新时间:2024-10-11 21:23:41
为什么char和bool在c ++中的大小相同?(Why is a char and a bool the same size in c++?)

我正在阅读C ++编程语言。 其中Stroustrup指出sizeof(char) == 1和1 <= sizeof(bool) 。 具体取决于实施。 为什么像布尔这样简单的值与char相同的空间?

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).

更多推荐

本文发布于:2023-08-04 18:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   char   bool   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!