为什么一个char和一个bool在C ++中相同的大小? [英] Why is a char and a bool the same size in c++?

查看:216
本文介绍了为什么一个char和一个bool在C ++中相同的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的 C ++编程语言。的在它斯特劳斯指出,的sizeof(char)的== 1 1 LT; = sizeof的(布尔)。具体情况取决于执行。为什么这样一个简单的值作为布尔采取相同的空间作为一个字符?

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

这篇关于为什么一个char和一个bool在C ++中相同的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆