为什么在 C++ 中 char 和 bool 的大小相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/266870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:09:58  来源:igfitidea点击:

Why is a char and a bool the same size in c++?

c++boolean

提问by 108

I'm reading The C++ Programming Language.In it Stroustrup states that sizeof(char) == 1and 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?

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

回答by Cybis

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

在现代计算机体系结构中,字节是内存的最小可寻址单位。将多个位打包成一个字节需要应用额外的位移操作。在编译器级别,这是内存与速度要求之间的权衡(在高性能软件中,那些额外的位移操作可能会不必要地增加和减慢应用程序的速度)。

回答by Robert Gamble

Because in C++ you can take the address of a boolean and most machines cannot address individual bits.

因为在 C++ 中,您可以获取布尔值的地址,而大多数机器无法寻址单个位。

回答by Kibbee

It takes the same space, because the smallest amount of space you can write in memory is a single byte. Both values are stored in a byte. Although you theoretically only need 1 bit to signify a boolean value, you still have to have a whole byte to store the value.

它占用相同的空间,因为您可以在内存中写入的最小空间量是单个字节。两个值都存储在一个字节中。虽然理论上你只需要 1 位来表示一个布尔值,你仍然必须有一个完整的字节来存储该值。

回答by Rob Walker

Theoretically you only need a single bit for a bool, but working with less than 1 byte's worth of data is messy. You need more instructions to achieve anything and you don't really benefit.

从理论上讲,您只需要一个位来表示布尔值,但是处理少于 1 个字节的数据是很麻烦的。你需要更多的指令来完成任何事情,而你并没有真正受益。

If you want to pack multiple booleans into a single byte you can use a bit-field structure.

如果您想将多个布尔值打包成一个字节,您可以使用位域结构

回答by James Curran

Actually, in most implementation that I know of sizeof(bool) == sizeof(int). "int" is intended to be the data size that is most efficient for the CPU to work with. Hence things which do not have a specific size (like "char") are the same size as an int. If you had a large number of them per object, you may want to implement a means of packing them for storage, but during normal calculation, it should be left it's native size.

实际上,在我知道的大多数实现中,sizeof(bool) == sizeof(int)。“int”旨在成为 CPU 处理最有效的数据大小。因此,没有特定大小的事物(如“char”)与 int 的大小相同。如果每个对象有大量它们,您可能希望实现一种将它们打包以进行存储的方法,但在正常计算期间,应保留其原始大小。

回答by David Nehme

There is this thing in C++ called vector that attempts to exploit the fact that you can theoretically store 8 bools in one char, but it's widely regarded as a mistake by the C++ standards committee. The book "effective stl" actually says "don't use it". That should give you an idea of how tricky it is.

C++ 中有一个叫做 vector 的东西,它试图利用理论上可以在一个字符中存储 8 个布尔值的事实,但它被 C++ 标准委员会广泛认为是一个错误。书“有效的stl”实际上说“不要使用它”。这应该让您了解它有多棘手。

BTW: Knuth has a bookjust dedicated to bitwise operations. Boost also has a librarydedicated to handling large numbers of bits in a more memory efficient way.

顺便说一句:Knuth 有一专门介绍按位运算的。Boost 还有一个专门用于以更高效的内存方式处理大量位的

回答by sep

A byte is the smallest addressable unit of memory.

字节是最小的可寻址内存单元。

Consider the following code:

考虑以下代码:

    bool b[9];
    bool *pb0 = &b[0];
    bool *pb1 = &b[1];

    for (int counter=0; counter<9; ++counter)
    {
         // some code here to fill b with values
         b[counter] = true;

    }

If bool is stored as 1 bit, then pb0 will equal pb1, because both have the same address. This is clearly not desirable!

如果 bool 存储为 1 位,则 pb0 将等于 pb1,因为两者具有相同的地址。这显然是不可取的!

Additionally the assignment in the loop will result in non-trival assembly code. It will involve a different bit shift in each iteration of the loop. In high-performance software, those extra bit-shift operations can slow down the application needlessly.

此外,循环中的赋值将产生非平凡的汇编代码。它将在循环的每次迭代中涉及不同的位移位。在高性能软件中,那些额外的位移操作会不必要地减慢应用程序的速度。

The STL library provides a work-around in situations where space DO matter. The use of std::vector<bool> will store bool as 1 bit. The paradox of the example above do not apply because

STL 库在空间很重要的情况下提供了一种解决方法。使用 std::vector<bool> 会将 bool 存储为 1 位。上面例子的悖论不适用,因为

  • the overloading of operator[] hides the rigors of the bit shift operation
  • the use of iterators instead of pointers give additional flexibilty to the implementation
  • operator[] 的重载隐藏了位移操作的严格性
  • 使用迭代器而不是指针为实现提供了额外的灵活性