In C how much space does a bool (boolean) take up? Is it 1 bit, 1 byte or something else?

▼魔方 西西 提交于 2020-08-21 07:54:30

问题


In C how much space does a bool (boolean) take up? Is it 1 bit, 1 byte or something else? Does it matter if the program is 32-bit or 64-bit?


回答1:


If you are referring to C99 _Bool try:

printf("%zu\n", sizeof(_Bool)); /* Typically 1. */

Note the standard says:

6.2.5

An object declared as type _Bool is large enough to store the values 0 and 1.

The size cannot be smaller than one byte. But it would be legal to be larger than one byte.




回答2:


The smallest addressable "thing" in C is a char. Every variable in C must have a unique address, therefore your bool can't be smaller than that. (Note that char isn't always 8 bits though)




回答3:


In older C standards, there was no such type defined. Many embedded microcontrollers, however, include special circuitry to allow for efficient processing of single-bit flags; some allow for such processing of variables stored anywhere, while others only allow it for variables stored in a particular region of memory. Compilers for such processors allow individual variables of static duration to be declared as type "bit"; such variables will generally only take one bit of storage (allocated, if necessary, within a region that can accommodate such usage). Some compilers will allow automatic variables of non-recursive routines to be declared as 'bit' type, but will silently regard them as 'static' (the linkers provided with such compilers require that routines identify which other routines they call, and will reject programs in which routines that are not tagged as re-entrant call each other in mutually-recursive fashion).

A few points worth noting:

  1. Processors whose compilers support "true" bit variables can generally set, clear, or branch upon the values of such variables faster and with less code than they could set, clear, or branch upon byte-wide flags;
  2. Many such processors have very small amounts of RAM. On many processors, question of whether individual variables (as distinct from array elements or structure fields) take a bit or a byte each wouldn't be worth worrying about. On a processor with 25 bytes of memory, however, there's a huge difference between having 16 flags taking one byte each, versus having all 16 flags combined into two bytes.
  3. At least on compilers I've seen, bit variables may not be used as structure fields nor array elements, nor may one take the address of one.

I don't know enough about C99 or later versions of the C or C++ standards to know whether they have any concept of a standalone bit type which doesn't have an address. I can't think of any reason such a thing shouldn't be possible, especially the standards already recognize the concept of things like structure bit-fields which behave much like lvalues but don't have addresses. Some linkers may not support such variables, but such linkers could be accommodated by making their actual size implementation-dependent (indeed, aside from program speed or total memory usage, it would be impossible to tell whether such variables were given 1 bit or 64 bits each).




回答4:


The exact size of a boolean will be compiler-specific but will always be at least one byte.

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




回答5:


it depends on your compiler. some will take 1 byte, some the size of a int (sometime bool is just a typedef or #define of a int). I even saw bool as a short.

however don't expect it to be a bit. the necessity for any pointer to be cast-able to void* then back and keep same value makes that impossible as void* addresses bytes. BTW this is one reason why individual fields (as in int myvalue:2) cannot be addressed.

there is usually no difference in 32 or 64 build as 32 or 64 bits there is related to pointer size.




回答6:


It usually takes up one byte (8 bits). The usual code I use to make sure type sizes are what I think they are follows. The sample output in the comment says my char is 1 byte (8 bits), and the same for bool.

/**
 * using gcc, you can compile this with the following command:
 *   g++ -otype-sizes type_sizes.cpp
 * and then run with with
 *   ./type-sizes
 *
 * output on my 64bit linux machine follows. Note that
 * the not-so-primitive types are reporting size on
 * the stack (the actual data in on the heap and is
 * not reported by sizeof()). To get the "length" of
 * these you can use vector<>::size() or string::length().

bits in a single char: 8

Sizes of primitive types:
  char:        1
  bool:        1
  short:       2
  int:         4
  long:        8
  long long:   8
  float:       4
  double:      8
  long double: 16

  Not so primitive types:
    string(""): 8
    string("Hello, World!"): 8
    vector<int>(0): 24
    vector<int>(10): 24

 *
 **/

#include <climits>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    cout << "bits in a single char: " << CHAR_BIT << endl
    << endl
    << "Sizes of primitive types:\n"
    << "  char:        " << sizeof(char) << endl
    << "  bool:        " << sizeof(bool) << endl
    << "  short:       " << sizeof(short) << endl
    << "  int:         " << sizeof(int) << endl
    << "  long:        " << sizeof(long) << endl
    << "  long long:   " << sizeof(long long) << endl
    << "  float:       " << sizeof(float) << endl
    << "  double:      " << sizeof(double) << endl
    << "  long double: " << sizeof(long double) << endl
    << endl
    << "  Not so primitive types:\n"
    << "    string(\"\"): " << sizeof(string("")) << endl
    << "    string(\"Hello, World!\"): " << sizeof(string("Hello, World!")) << endl
    << "    vector<int>(0): " << sizeof(vector<int>(0)) << endl
    << "    vector<int>(10): " << sizeof(vector<int>(10)) << endl
    << endl;
}



回答7:


It doesnt matter whether you are in 32-bit or 64-bit, that's the size of the instructions to the processor, completely different matter.

A bool takes in real 1 bit, as you need only 2 different values. However, when you do a sizeof(bool), it returns 1, meaning 1 byte. For practical reasons, the 7 bits remaining are stuffed.

you can't store a variable of size less than 1 byte.

-> bool takes up 1 byte




回答8:


For the people who says you cannot store only 1 bit, you can using bitsets. Bitsets are a special type of data structure that stores each bit with a size of only 1 bit instead of 1 byte. It stores it in the stack memory so, you must define a size for it. Read more about it here




回答9:


There is no boolean type. When branching, a value of zero is treated as false and all others as true.

The smallest type in c is char, which takes up one byte on almost every system. It is possible to use the bits of a char to store eight boolean values.



来源:https://stackoverflow.com/questions/8014161/in-c-how-much-space-does-a-bool-boolean-take-up-is-it-1-bit-1-byte-or-someth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!