1

The size of a bool is claimed to be 1 byte using this command:

printf("%ld\n", sizeof(bool));

But this doesn't seem very efficient. So how are bools stored in memory?

Will the OS search for other bools and store up to 8 bools in a byte, will each bool be stored in a single byte, resulting in 7 unused bits or are they stored differently?

user11914177
  • 885
  • 11
  • 33
  • 2
    The answer depends a lot on the compiler and system. I guess you can find your answer by reading about padding/aligment of c++ compilers, wikipedia talks about it https://en.wikipedia.org/wiki/Data_structure_alignment – stalker2106 Aug 14 '19 at 14:05
  • 3
    The smallest piece of memory that can be addressed is a byte, so packing Booleans creates other problems that are likely to be less efficient in terms of execution time. [`vector` helps illustrate some of these](https://stackoverflow.com/a/17794965/10077). – Fred Larson Aug 14 '19 at 14:06

0 Answers0