A very weird bool...
Hi,
I have a class in which I define a boolean value and a method like this:
bool myValue;
bool getMyValue();
and I have:
bool CMyClass::getMyValue()
{
return(myValue);
}
What is very weird is that my returned value can be something different from 0 or 1 (205 for instance!). I checked it with the debugger and couldn''t believe my eyes!!!!
I know that if you use the window''s ''BOOL'' these things can happen, but not with a pure boolean type!!!
This seems to happen only with one of my many boolean values. What can be the cause?
Thanks
You''re not initializing the value, and it''s getting set to the default debug-allocated-memory value of 0xCD (205 in decimal). Try initializing your values in the constructor.
DerekSaw:
No, I checked but I don''t have any #define bool BOOL
Also when I write:
myValue=2;
My compiler says:
warning C4305: ''='' : truncation from ''const int'' to ''bool''
Which shows that it is a ''true'' bool type.
Assassin:
Shouldn''t the default value be either 0 or 1??? Even in debug mode! But you are right, my value was not initialized.
No, I checked but I don''t have any #define bool BOOL
Also when I write:
myValue=2;
My compiler says:
warning C4305: ''='' : truncation from ''const int'' to ''bool''
Which shows that it is a ''true'' bool type.
Assassin:
Shouldn''t the default value be either 0 or 1??? Even in debug mode! But you are right, my value was not initialized.
When the memory is allocated your bool is automatically set to 0xCD like floating said. What you do is inside your class''s constuctor is myValue = false; or whatever you want it to be. A bool takes up a byte in the memory and so when initialized to with the defaults, it is 0xCD... you aren''t allowed to set it to such a value.
Make any sence?
"The only thing worse than not having that new _______ , is when some rich kid has it, but can''t and/or doesn''t appreciate it."-me
Tazzel3d ~ Dwiel
Make any sence?
"The only thing worse than not having that new _______ , is when some rich kid has it, but can''t and/or doesn''t appreciate it."-me
Tazzel3d ~ Dwiel
quote:Original post by Assassin
You're not initializing the value, and it's getting set to the default debug-allocated-memory value of 0xCD (205 in decimal). Try initializing your values in the constructor.
My debugger shows one or zero only for unintialized bool variables. Compiler: MSVC5. I don't think the 0xCD thingy applies for bool variables... Well of course, unless the bool is being defined, I believed.
500x7
[EDIT] Maybe there's a compiler setting to treat bool as int turn on?? (guessing )
[edited by - DerekSaw on January 21, 2003 10:22:03 PM]
In debug mode with MSVC, all locals are initialised to 0xCD. A float, for example, will have whatever value equates to a bit pattern of 0xCDCDCDCD.
quote:Original post by Floating
Shouldn''t the default value be either 0 or 1???
Only if the variable is initialised. One of C++''s stupid rules is that local POD variables do not get initialised. Accessing the value of an uninitialised variable leads to undefined behaviour, the results of which might include bools getting set to 0xcd, demons flying out of your nostrils, or even something apparently sensible.
quote:Original post by SabreMan
demons flying out of your nostrils
I tried that, but Visual Studio .NET doesn''t seem to support nostrils.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement