-4

As we already know, if we don’t initialize a declared value, it’ll contain a random piece of information found in the RAM. Now, what if we’re talking about a bool? Is it false (0) by default or could it also be randomly a true (1) value, too?

N.T.
  • 99
  • 6
  • The same rule applies to `bool` variables, their value will be indetermined. What makes you doubt? – πάντα ῥεῖ Mar 09 '19 at 17:09
  • Note that some compilers will initialize values if you compile in debug, including booleans. – Joakim Thorén Mar 09 '19 at 17:12
  • 1
    "Random" as in "do not assume anything" yes. "Random" as in "good for making guessing games" no. – Yunnosch Mar 09 '19 at 17:14
  • @NeilButterworth No need to be rude, I am not sure whether that applies to the booleans as well, since they can be either true or false. Anytime I used an uninitialized boolean it was always false. – N.T. Mar 09 '19 at 17:14
  • Ich bit of an int can be true or false. Does that make you doubt about their randomness? – Yunnosch Mar 09 '19 at 17:14
  • @Yunnosch That makes sense. Thanks. – N.T. Mar 09 '19 at 17:15
  • However, slightly confusing when compared to my previous comment, are you aware how exactly a boolean is stored? – Yunnosch Mar 09 '19 at 17:16
  • @Yunnosch As we’re taught in class - when declaring a variable, a certain space in the RAM is saved for that piece of information, if boolean it’s 1B since it’s just 1 or 0 let’s say, and if not initialized or given a value, there will be a random value saved in it. I was uncertain whether it applied for booleans since it’d always return a value of 0 when uninitialized. – N.T. Mar 09 '19 at 17:18
  • In contrast to what you wrote, for a boolean it is only defined that "false" will be stored as 0. Mostly that means that any other value, e.g. in an 8bit 1-255, is considered true. Not sure how much of that is standardized. – Yunnosch Mar 09 '19 at 17:18
  • Depending on what you mean by "1B".... A single bit cannot be allocated/linked. A single Byte can. – Yunnosch Mar 09 '19 at 17:19
  • @Yunnosch I meant 1 byte, it’s 8 bits if I’m not wrong. – N.T. Mar 09 '19 at 17:22
  • 100% certain this is undefined behavior. My team recently hit [this exact bug from a compiler optimization on MacOS](https://stackoverflow.com/questions/54120862/does-the-c-standard-allow-for-an-uninitialized-bool-to-crash-a-program/54125820) as a result of an uninitialized `bool`. The address contents of the bool was neither 0 nor 1, but instead uninitialized with a rand stack value of 109. And the compiler was using its byte value to compute an address from the ternary statement. Oops. – selbie Mar 09 '19 at 17:30
  • Don't assume that uninitialized bools are always false just because an uninitialized bool ended up being false a couple of times when you tested it. Uninitialized variables contain an intermediate value, and reading it is [undefined behavior](https://en.cppreference.com/w/cpp/language/ub). Therefore, anything can happen. – eesiraed Mar 09 '19 at 18:06

1 Answers1

-2

Booleans are primitive data types without a default value. No primitive data type has a default value.

Also, not initialising a declared variable does not necessarily make its value random. It depends on whether your data type has a default constructor (one that takes no arguments, and is implicitly called) that initialises it or not. Plain old data types (to which primitive types belong) have an "empty" default constructor that does not assign any initial values. In contrast, types like std::string have a non-empty default constructor that initialises a string to a well-defined state.

In contrast to ints and other primitive types however, booleans do have a special conversion operator: To convert anything to a bool, it is first checked whether that value is 0 or not, and depending on the result, a 0 or non-0 byte is returned (as usually, bools are implemented as bytes). I am not sure whether there even is one single byte representation for true, or whether any non-0 byte is a true bool.

RmbRT
  • 460
  • 2
  • 10