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
6 2195
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.
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.
Perfect - This explains a lot. Thanks for your advice :)
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
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
| |