Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,
I assumed that bool variable can hold either true or false values by default,but when I ran this code snippet I got to know that default value is undefined.
I tried to print the bTest value and got different outputs at each run.Does that mean that bool is equivalent to unsigned char?
int main() {
bool bTest;
if(bTest != true && bTest != false)
printf("bool default value is undefined - %d",bTest);
return 0;
)
Posted
Updated 13-May-11 20:06pm
v2

Internally, a bool is stored as a byte, and (like any other uninitialized variable) it will have a "random" value - in practice this is hex CC - which does not match the value of true (1) or false (0)

While it would be more space efficient to use only a single bit to store a bool, it is far more processor efficient to use a byte: bit operations need more assembly steps than byte!
 
Share this answer
 
Comments
ZeeroC00l 14-May-11 2:50am    
A small question regarding the same,
is this behavior because of the fact that the intrinsic types don't invoke default constructors by themselves ?

And I tested the bool type without initializing, every time i compile and run it i end up getting a default value of 191 for bool type. Any reason why ? If it were to be random why not get a different value every time I compile and run ?
OriginalGriff 14-May-11 3:10am    
No, it's more simple than that: it is unassigned memory. No value is given to it at all - you get what it has and that's all: if you try the same with any unassigned value you will get rubbish. The compiler doesn't waste processing type initializing values, instead it complains at compile time when you try to use them before putting a value in, when it can spot it! Another good reason for setting "treat warnings as errors"! :laugh:
That's why I put the random in double quotes - if you don't assign a value, then you get the value currently on the stack (or heap if you manage to put it there) when the variable is allocated space. Depending on what else you program has been doing, the value may or may not change.
ZeeroC00l 14-May-11 3:14am    
Yes, as you said its better to treat warnings as errors...
And I got your point.
Thanks for the Clarification :)
OriginalGriff 14-May-11 3:18am    
You're welcome!
Richard MacCutchan 14-May-11 5:02am    
I think the value of xCC is actually placed in all uninitialised memory in Debug mode applications, in order to help find such bugs.
Hi all,
thanks for your replies,so just to conclude - bool can have the value present in the memory space it is assigned with,in debug mode it gives a warning - that variable is uninitialized ,in release we have to make sure we are using it assigned.
 
Share this answer
 
Comments
Stefan_Lang 16-May-11 5:20am    
You _always_ should initialize variables, no matter what type they are. And the best place to do this is where you declare the variable.

This is a 'best practice' commonly known as RAII (Resource Acquisition Is Initialization). See this Wikipedia Article for more info:
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

FYI, in debug mode you will often not see bad effects from variables that you left uninitialized. This is true because many compilers *do* initialize all memory, usually by setting it to 0. There will thus usually be no warning or run-time error in debug mode. The warning others mentioned is a compiler warning: you should see that warning at compile time, provided you didn't set your options to ignore certain or all warnings.

In release mode, the compilers do not add the hidden code for initializing memory, so any errors due to initializatino failure will happen only then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900