71

I had declared a Boolean variable bool abc; in a class and thought that it would be false by default. An if condition in my program, if (abc), turned out to be true, so I output the value of abc, and saw that it contained the value 55. Is this normal?

Do we always have to assign 'bool abc=false' to be sure that it is false?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nav
  • 19,885
  • 27
  • 92
  • 135
  • For why: http://stackoverflow.com/questions/1910832/c-why-arent-pointers-initialized-with-null-by-default/1910992#1910992 – Martin York Jan 07 '11 at 08:52
  • [Note that reading an uninitialized variable leads to UB.](http://stackoverflow.com/questions/4259885/why-do-i-see-strange-values-when-i-print-uninitialized-variables/4259991#4259991) Just don't do it. – GManNickG Jan 07 '11 at 23:02
  • 2
    An 'interesting' bug that can show up in this situation is both b and !b evaluating to true – Matthew Finlay Aug 02 '12 at 09:06

5 Answers5

69

Talking about primitive built-in data types (bool, char, wchar_t, short, int, long, float, double, long double), according to C++ standard, only global variables get a default value of zero if they are not explicitly initialized.

For local variables it's not required for the complier to clean up the content of the memory they are assigned to. A local variable -- if not explicitly initialized -- will contain an arbitrary value.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nylon Smile
  • 8,990
  • 1
  • 25
  • 34
  • 8
    Technically it is variables of `static storage duration` globals are a member of this type of variable. – Martin York Jan 07 '11 at 08:50
  • C++ will initialize built-in types to zero when they are initialized with a default constructor. Eg. in maps. `std::map m; std::cout << (m[0] ? "true": "false");` will always print `false`. – OhJeez Apr 09 '21 at 17:01
  • It depends on the compiler. Variables are not initialized by default. – Monza Jun 10 '22 at 02:44
49

Yes, you should always initialize your variables. Until you intimately learn the times when it is and isn't necessary to do so explicitly, you should do it all the time, no matter what. And by then...well...why stop a good habit?

To initialize a bool to false it's sufficient to default construct it:

struct X
{
  bool b;
  X() : b() {}
};
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • 17
    I like to add the explicit boolean literal false (to me that makes it more readable (though technically no different from yours)). – Martin York Jan 07 '11 at 08:48
  • 1
    is the init list necessary? I thought the expression 'bool b;' did invoke the default ctor... I've always been confused about how init lists and member variables interact – CCJ Jan 05 '21 at 19:41
  • 1
    @CCJ it is in order to initialize all class members. Unless, you initialize them in-place in the class (like `bool b = false`) - which is mostly only viable for fundamental types. – KeyC0de Nov 28 '22 at 14:39
9

Only global variables are assigned 0 (false) by default. Any local variables are given a non-zero garbage value, which would evaluate to true in a boolean variable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Regan Koopmans
  • 451
  • 6
  • 16
5

Yes. You need to either do bool x=false or bool x(false). Primitives that are not initialized may have any value.

See Wikipedia

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58
4

Yes. Always initialize your variables before use. Even if the language guarantees that they will have specific values. If you can't force yourself, get a compiler that will complain, and then make yourself do that. :)

However, don't initialize values unless it really has a meaning for them to be initialized. For example, if you have a loop like this (I'm not saying this is good code, it's just an example):

int i = 0;
while ((i = getNum()) == 5)
{
}

Don't initialize i to zero like I did. It makes no sense, and while it shuts up the compiler, it introduces the possibility that you'll forget it and then your code will be messed up. If you can force yourself to initialize only at the right times -- no more, no less -- then you'll make debugging a heck of a lot easier, since your wrong code will look wrong even at just a glance.

So, in one line: Never initialize just to prevent the compiler from complaining, but always initialize before use.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user541686
  • 205,094
  • 128
  • 528
  • 886