Jump to content

View more

Image of the Day

Isn't this a lovely apple tempart placeholder thing  #gamedev worth a #screenshotsaturday even I would say. https://t.co/fQH1d0ySIG
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

C++ Default value for bool

2: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
16 replies to this topic

#1 Matt77   Members   

122
Like
0Likes
Like

Posted 27 May 2010 - 03:12 AM

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.

#2 SiCrane   Moderators   

11706
Like
0Likes
Like

Posted 27 May 2010 - 03:15 AM

No, primitives, including bool, are not guaranteed to be initialized to any particular value in the general case.

#3 Matt77   Members   

122
Like
0Likes
Like

Posted 27 May 2010 - 03:16 AM

i see.
Don't know where/when i formed this idea about it. Glad i've run into this.

thanx SiCrane.

#4 andur   Members   

781
Like
1Likes
Like

Posted 27 May 2010 - 03:17 AM

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.

#5 filipe   Members   

152
Like
0Likes
Like

Posted 27 May 2010 - 03:46 AM

I think bools are initialized to 0 if they're globals.

#6 rmed002   Members   

100
Like
0Likes
Like

Posted 27 May 2010 - 03:58 AM

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.


#7 pauls_1979   Members   

183
Like
1Likes
Like

Posted 27 May 2010 - 03:58 AM

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.

#8 Josh Petrie   Moderators   

12532
Like
0Likes
Like

Posted 27 May 2010 - 04:08 AM

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).

#9 SiCrane   Moderators   

11706
Like
0Likes
Like

Posted 27 May 2010 - 04:25 AM

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.

#10 Kwizatz   Members   

1377
Like
0Likes
Like

Posted 27 May 2010 - 04:32 AM

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?

#11 szecs   Members   

2915
Like
0Likes
Like

Posted 27 May 2010 - 06:52 AM

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)

#12 DevFred   Members   

840
Like
0Likes
Like

Posted 27 May 2010 - 07:10 AM


bool array[100] = {};


#13 Washu   Senior Moderators   

7782
Like
0Likes
Like

Posted 27 May 2010 - 07:33 AM

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.
ScapeCode - Blog | SlimDX


#14 szecs   Members   

2915
Like
0Likes
Like

Posted 27 May 2010 - 08:12 AM

Quote:
Original post by DevFred

bool array[100] = {};
Hmmm. I learn something new every day. Thanks guys!

#15 clashie   Members   

632
Like
0Likes
Like

Posted 27 May 2010 - 10:49 AM

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...

#16 SiCrane   Moderators   

11706
Like
0Likes
Like

Posted 27 May 2010 - 11:01 AM

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.

#17 Flawe   Members   

114
Like
0Likes
Like

Posted 27 May 2010 - 09:18 PM

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.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.