Is Sizeof(Bool) Defined in the C++ Language Standard

Is sizeof(bool) defined in the C++ language standard?

sizeof(bool) is implementation defined, and the standard puts notable emphasis on this fact.

§5.3.3/1, abridged:

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type is implementation-defined. [Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69)]

Footnote 69):

sizeof(bool) is not required to be 1.

Why the sizeof(bool) is not defined to be one, by the Standard itself?

Many platforms cannot effectively load values smaller than 32 bits. They have to load 32 bits, and use a shift-and-mask operation to extract 8 bits. You wouldn't want this for single bools, but it's OK for strings.

Is bool a native C type?

bool exists in the current C - C99, but not in C89/90.

In C99 the native type is actually called _Bool, while bool is a standard library macro defined in stdbool.h (which expectedly resolves to _Bool). Objects of type _Bool hold either 0 or 1, while true and false are also macros from stdbool.h.

Note, BTW, that this implies that C preprocessor will interpret #if true as #if 0 unless stdbool.h is included. Meanwhile, C++ preprocessor is required to natively recognize true as a language literal.

C++ : why bool is 8 bits long?

Because every C++ data type must be addressable.

How would you create a pointer to a single bit? You can't. But you can create a pointer to a byte. So a boolean in C++ is typically byte-sized. (It may be larger as well. That's up to the implementation. The main thing is that it must be addressable, so no C++ datatype can be smaller than a byte)

Is there a fixed-width bool type in standard C++?

There is not. Just use uint8_t if you need to be sure of the size. Any integer type can easily be treated as boolean in C-related languages. See https://stackoverflow.com/a/4897859/1105015 for a lengthy discussion of how bool's size is not guaranteed by the standard to be any specific value.

What is the minimum size of a boolean?

What is the minimum size of a boolean?

in standard you can read 5.3.3:

The result of sizeof applied to any other fundamental type (3.9.1) is
implementation-defined. [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and
sizeof(wchar_t) are implementation-defined.75 —end note ]

and note:

sizeof(bool) is not required to be 1

so it might be 1 byte but it might be also 4 bytes. Standard also allows for ints to be of size 1 byte 16bits:

1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

so minimum size for bool is 1 byte, the same as for int. The other question is whether you will ever find platform with 1 byte int type.

[edit]

minimum size (guaranteed minimum) for int is 16bits, this size guarantee for integral/arithmetic types in C and C++ explains why.

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

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.



Related Topics



Leave a reply



Submit