Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Floating

A very weird bool...

This topic is 5688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
demons flying out of your nostrils



I tried that, but Visual Studio .NET doesn''t seem to support nostrils.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!