Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Default value for bool

Started by Matt77 May 27, 2010 at 9:12 AM 15 replies 42.6k views
Original Post
Matt77
Matt77
Hello everyone! I always thought that the default value for a bool was false. But it turned out i discovered that it gets true sometimes, and it depends on the machine it is running.

#include <cstdio>

int main()
{
	bool l_default_value;

	if(l_default_value)
		printf("default value for bool is true\n");
	else
		printf("default value for bool is false\n");

	return 0;
}
Is there a default value for a bool in C++? Why this happens? TIA.
SiCrane
SiCrane
No, primitives, including bool, are not guaranteed to be initialized to any particular value in the general case.
Matt77
Matt77
i see.
Don't know where/when i formed this idea about it. Glad i've run into this.

thanx SiCrane.
andur
andur
There are no default values in C++ for primitive types. You are just getting whatever happened in be in memory at that location. Debugger's will usually initialize stuff to default values.
filipe
filipe
I think bools are initialized to 0 if they're globals.
rmed002
rmed002
Quote:
Original post by filipe
I think bools are initialized to 0 if they're globals.

It would seem to apply for all globals then.
At least my combination of bools and ints all got zero as their initial values.
Or its just that, the whole datasegment is just by default zeroed?

This happened on Visual Studios debug mode though...
Wonder if enabling optimizations and disabling runtime checkups would alter the result.
pauls_1979
pauls_1979
Quote:
I think bools are initialized to 0 if they're globals.


Under no circumstances would I recommend you ever rely on this being the case.
jpetrie
jpetrie
Quote:

Under no circumstances would I recommend you ever rely on this being the case.

It is reliable; objects with static storage duration are zero-initialized before all other initialization occurs (see 'basic.start.init' in the standard).
SiCrane
SiCrane
Only for certain values of "reliable" though. Try running this with MSVC 2008:
struct Foo {};int Foo::* data;int main() {  std::cout << (data == 0) << std::endl;}

I'm firmly in the "just initialize your variables" camp.
Kwizatz
Kwizatz
Just don't be lazy, if you need something initialized to a particular value, do it explicitly yourself, it really isn't that much of a deal to write:

bool var = false;


instead of

bool var;


or is it?
szecs
szecs
Quote:
Original post by Kwizatz
Just don't be lazy, if you need something initialized to a particular value, do it explicitly yourself, it really isn't that much of a deal to write:

bool var = false;


instead of

bool var;


or is it?
It is, when you have an array. (Real programmers probably cry in pain while reading this)
DevFred
DevFred
bool array[100] = {};
Washu
Washu
Quote:
Original post by DevFred
bool array[100] = {};

bool* array = new bool[100]();
too...

In general, always initialize your variables, never just leave it up to the compiler (even for statics), because interpretations of the standard varies from compiler to compiler, and even version to version. Just spare yourself the hassle and initialize them yourself.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
clashie
clashie
Quote:
Original post by pauls_1979
Quote:
I think bools are initialized to 0 if they're globals.


Under no circumstances would I recommend you ever rely on this being the case.


afaik, this is something you can rely on. anything with static duration will be 0'd, guaranteed.

static int magic[32] = { 0, 1, 2, 3 };

will end up 01230000...
SiCrane
SiCrane
Quote:
Original post by clashie
anything with static duration will be 0'd, guaranteed.

Quote:
Original post by SiCrane
Try running this with MSVC 2008:
struct Foo {};int Foo::* data;int main() {  std::cout << (data == 0) << std::endl;}


Quote:

static int magic[32] = { 0, 1, 2, 3 };

will end up 01230000...

Which doesn't rely on the zero-initialization of objects with static storage duration. If you specify four items in an aggregate initializer list for an array of 32 ints, you aren't saying leave the items you didn't specify uninitialized, you're saying initialize the rest of the other items with 0.
TinyGameDev
TinyGameDev
Uninitialized statics are stored in the .bss section of the program and the loader knows to initialize these variables to 0. However, if the static is actually placed in the .bss section might be compiler specific so to be safe you should probably just initialize it yourself.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.