473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Strange Behaviour initializing bool

11
Hi,

I was under the impression that bool's were false by default... Am I wrong about this?

In VC++6, when I define a bool in a class header (i.e. bool testbool) and then access it BEFORE I have initialized it to a definite value, it returns 1. I am sure that I have not accidentally initialized it to true anywhere. Just curious, is it supposed to be this way?

Cheers,
Crispin
Feb 26 '07 #1
6 2195
sicarie
4,677 Expert Mod 4TB
Hi,

I was under the impression that bool's were false by default... Am I wrong about this?

In VC++6, when I define a bool in a class header (i.e. bool testbool) and then access it BEFORE I have initialized it to a definite value, it returns 1. I am sure that I have not accidentally initialized it to true anywhere. Just curious, is it supposed to be this way?

Cheers,
Crispin
C/C++ considered any number not 0 to be 1, and will return that value unless the memory location it allocates to the boolean value is all 0's, which is not likely.

Basically - it creates the space for the variable in memory, and that space holds whatever value is there, before you initialize it. Once you initialize it, that value is wiped out, but before you do, you are accessing memory that is part of some other program or file's temp space, that is now free.
Feb 26 '07 #2
willakawill
1,646 1GB
Right. This is the case with all variables in c++. Unless you give them an initial value you are left with the garbage at that particular memory address.
Feb 26 '07 #3
crispin
11
Perfect - This explains a lot. Thanks for your advice :)
Feb 27 '07 #4
Hi,

I was under the impression that bool's were false by default... Am I wrong about this?

In VC++6, when I define a bool in a class header (i.e. bool testbool) and then access it BEFORE I have initialized it to a definite value, it returns 1. I am sure that I have not accidentally initialized it to true anywhere. Just curious, is it supposed to be this way?

Cheers,
Crispin
since bools can take only two values
by default it should be zero .due some mistake in the system hardware it might happen
Feb 27 '07 #5
sicarie
4,677 Expert Mod 4TB
since bools can take only two values
by default it should be zero .due some mistake in the system hardware it might happen
That's not true, actually - there is nothing wrong with the OP's hardware.

Check it for yourself - I just did (though for some reason I get an error when I try to copy and paste, so can't copy my code...) But if you initialize four values to: any arbitrarily large number, one that is large and negative, one to one, and one to zero, then put them all in if statements that print, the only one in gcc that will not print is the one with 0. This is a compiler quirk, not a hardware problem.
Feb 27 '07 #6
willakawill
1,646 1GB
since bools can take only two values
by default it should be zero .due some mistake in the system hardware it might happen
Nonsense. The compiler has to treat any non zero value as true or 1. This is called undefined behaviour because we don't know what will be at that memory address without initializing the variable, bool or otherwise.
Feb 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: muser | last post by:
I have written a function that checks the first four characters in an address are valid; i.e. 1d2 sour st would prove to be invalid. bool checkdigitsinaddress( const char* string ) { for( int...
3
by: Joe | last post by:
Hi, I have been struggling with this issue for a couple of days and would like to know if some can give me a pointer. I want to initialize a struct with default values and depending on the...
20
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
23
by: Saizan | last post by:
Why subclassing bool from int either __invert__ or __neg__ haven't been overrided to produce a boolean negation? I suspect backwards compatibility or something alike, but I still wonder.. And...
10
by: Michele | last post by:
Please forgive me for the neverending code down here but I cannot find a rational explanation of the output of this simple program (really!). Soluzione class has a double field to represent a...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
3
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member...
3
by: rs387 | last post by:
Hi, I've found the following behaviour on importing a variable from a module somewhat odd. The behaviour is identical in Python 2.5 and 3.0b2. In summary, here's what happens. I have a...
5
by: meLlamanJefe | last post by:
I have written a Queue class for our use that stores pointers to what I call RequestObjects. RequestObjects are a base class from which other types inherit and this allows the queue (or deque in this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.