-3

I came up with a thought about the types of _Bool/ bool (stdbool.h) in C and bool in C++.


We use the boolean types to declare objects, that only shall hold the values of 0 or 1. For example:

_Bool bin = 1;

or

bool bin = 1;

(Note: bool is a macro for _Bool inside the header file of stdbool.h.)

in C,

or

bool bin = 1;

in C++.


But are the boolean types of _Bool and bool really efficient?


I made a test to determine the size of each object in memory:

For C:

#include <stdio.h>
#include <stdbool.h>   // for "bool" macro.

int main()
{
    _Bool bin1 = 1;
    bool bin2 = 1; // just for the sake of completeness; bool is a macro for _Bool.    

    printf("the size of bin1 in bytes is: %lu \n",(sizeof(bin1)));
    printf("the size of bin2 in bytes is: %lu \n",(sizeof(bin2)));  

    return 0;
}

Output:

the size of bin1 in bytes is: 1
the size of bin2 in bytes is: 1

For C++:

#include <iostream>

int main()
{
    bool bin = 1;

    std::cout << "the size of bin in bytes is: " << sizeof(bin);

    return 0;
}

Output:

the size of bin in bytes is: 1 

So, objects of a boolean type do get stored inside 1 byte (8 bits) in memory, not just in one 1 bit, as it normally only shall require.

The reason why is discussed here: Why is a char and a bool the same size in c++?. This is not what my question is about.


My question are:

  • Why do we use the types of _Bool/ bool (stdbool.h) in C and bool in C++, if they do not provide a benefit in memory storage, as it is specificially pretended for use these types?

  • Why can´t I just use the types of int8_t or char (assuming char is contained of 8 bit (which is usually the case) in the specific implementation) instead?

Is it just to provide the obvious impression for a reader of the code, that the respective objects are used for 0 or 1/true or false purposes only?

Thank you very much to participate.

3 Answers3

4

Why do we use the types of _Bool/ bool (stdbool.h) in C and bool in C++, if they do not provide a benefit in memory storage, as it is specificially pretended for use these types?

You already mentioned the reason in your question:

We use the boolean types to declare objects, that only shall hold the values of 0 or 1

The advantage of using boolean datatype specifically is because it can only represent true or false. The other integer types have more representable values which is undesirable when you want only two.

Why can´t I just use the types of int8_t or char (assuming char is contained of 8 bit (which is usually the case) in the specific implementation) instead?

You can. In fact, C didn't have a boolean data type until C99 standard. Note that downside of using int8_t is that it is not guaranteed to be provided by all systems. And porblem with char is that it may be either signed or unsigned.

But you don't need to, since you can use boolean data type instead.


This implies that there is difference when I use trueor false with boolean types in comparison to when I use these with char or int8_t. Could you state this difference?

Consider following trivial example:

int8_t i = some_value;
bool b = some_value;

if (i == true)
if (i)
if (b == true)
if (b)

For int8_t, those two conditionals have different behaviour, which creates opporunity for the behaviour to be wrong if the wrong form is chosen. For a boolean, they have identical behaviour and there is no wrong choice.


P.S. If you want to compactly store multiple boolean values (at the cost of multiple instructions per read and write), you can use std::bitset or std::vector<bool> for example. In C there are no analogous standard library utilities, but such functionality can be implemented with shifting and masking.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

There's more than one sort of efficiency. Memory efficiency is one. Speed efficiency is another.

The original C language did not have a boolean type at all -- typically a programmer would use an int for boolean flags, 0 for false and 1 for true. If they were really concerned about memory efficiency, they might use a bitmap to store eight booleans in a byte, but this was generally only necessary in situations where memory was really scarce. But accessing an int is faster than accessing an int then unpacking its constituent bits.

_Bool/bool was introduced in C99. It reflects the common practice of storing booleans in an int.

However, it has the advantage that the compiler knows it's a boolean, so it's more difficult to accidentally assign it the value 3, add it to an integer, etc.

Most programming languages today store a boolean in a byte. Yes, it uses eight times more memory than necessary -- but it's fast, and it's rare to have so many booleans on the go at once that the waste becomes significant.

In many programming languages, the implementation is separate from the language spec -- Javascript's spec doesn't say how the Javascript runtime should store true or false. In C99 however you can rely on true being equivalent to integer 1.

If booleans are truly using too much of your system's memory, you can work with bitwise operations to store 8 booleans in an unsigned char or more in the larger types.

You can do that for runtime operations if necessary, or just when writing to an output format (if the problem is the size of records on filesystems, or network packets).

It's worth noting though, that in many, many modern applications, people are perfectly happy to represent false on the wire or on the filesystem as the 7 bytes [ '"', 'f', 'a', 'l', 's', 'e', '"' ]

slim
  • 40,215
  • 13
  • 94
  • 127
0

One of the reasons for having bool as well as int is to increase the comprehensibility of the code to those who come after and try to maintain it.

Consider these

bool b; int c;

if (b == c)

c = 2; b = 2;

Now, I'd have said that comparing a boolean (true or false) with a number is very likely an error. So things like 'if (b == 1)' could indicate a coding error. I hope you'd agree that 'b = 2' is just wrong.

Benefits in memory storage (and I don't recall anything anywhere claiming that using boolean types reduced memory requirements) are not the only reason for language features.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61