How is it possible that my program asked to cout a bool variable writes 104 on the display? Even if the variable wasn't initialized, it should display 0 or 1 I thought? Another thing is that it seems to be initialized and to go wild at one moment.

How is it possible that my program asked to cout a bool variable writes 104 on the display? Even if the variable wasn't initialized, it should display 0 or 1 I thought?

Well, you thought wrong there. Don't assume anything about uninitialized variables and their possible values at any given time. Instead initialize them to a known value/state as soon as possible.

It's because there is a mistake in the program.

Thank you for your very teaching answer. I'm far from saying that my question was very clear, but you certainly didn't need to post it. It was rude.

Well, you thought wrong there. Don't assume anything about uninitialized variables and their possible values at any given time. Instead initialize them to a known value/state as soon as possible.

The variable the program is supposed to write on the screen is almost certainly initalized. I'm guessing that maybe I'm not getting the right variable. But back to the unitialized variables, it would be good to know anything possible about their behavior to more easily recognize them. So my question is: when I have an unitialized boolean variable what can be the value of it? Does it depend on the compiler? I heard they are stored in one byte of memory. How are they stored and how are they read? How does the compiler interprete a sequence of bits when it supposes it's a boolean thing? Sorry about my English.

Edited 6 Years Ago by mmasny: n/a

The variable is almost certainly initalized. I'm guessing that maybe I'm not getting the right variable.

Shouldn't you be able to locate ALL uninitialized variables and fix the code? (just by going through the code, that is).

>> So my question is: when I have an unitialized boolean variable
>> what can be the value of it?
Depending on your compiler, the value may be initialized to a certain value, aiming to be helpful in detecting uninitialized variables. And depending on the compiler the value may differ between release/debug builds.

>> I heard they are stored in one byte of memory. sizeof(bool) will tell you the size on any platform.

>> How are they stored and how are they read?
>> How does the compiler interprete a sequence of bits when it supposes it's a boolean thing?
The question goes so deep that I'd suggest you to have your compiler generate assembler output and check what that tells you :)

The problem about finding out if it's initilized is that it's a field of an object that was dynamically created and that there are so many functions and methods involved that I'm simply getting lost and maybe don't understand something. But as far as I understand it, the field is initialized. The problem is very wierd to me:

//in a superclass of OknoWlasciwe
bool SprawdzCzyPrzeniesc(){return przenies;}   
//a method of OknoWlasciwe
void OknoWlasciwe::Wypisz(){
    cout<<SprawdzCzyPrzeniesc()<<endl;
} 
pulpit.DajPierwsze()->WezElement().Wypisz();
cout<<(pulpit.DajPierwsze()->WezElement().SprawdzCzyPrzeniesc())<<endl;

These two commands should - I think - do exactly the same thing. I don't see any reason for not being so. But the fact is that the first writes 0 on the display (which is what it's supposed do to) and the second writes 176 (which is certainly not what it's supposed to do). As I see it, they both should write the przenies field of the same object. They don't change it, so they should write the same. But they don't.

We're not physics. Post your code!

Its probably something wrong with your program. Maybe the "1" in "104"
is your boolean variable and the 04 is something else.

Comments
It's not "physics", it's "psychics" :o)

What compiler are you using?

DevC++. If you want I can paste more code, but it's going to be hard. I've just tried to extract the important parts and didn't succeed. But it's possible probably.

I assume you have GCC as your compiler? (Dev-C++ is only an IDE).
What compiler switches are you passing to GCC i.e. what is the compiler command line?

I assume you have GCC as your compiler? (Dev-C++ is only an IDE).
What compiler switches are you passing to GCC i.e. what is the compiler command line?

Sadly, I don't know even what you are talking about. I'm new to this. I just click compile and think it's enough... I don't pass anything to the compiler (at least that I would be aware of).

Edited 6 Years Ago by mmasny: even english is too hard for me

Sadly, I don't know even what you are talking about. I'm new to this. I just click compile and think it's enough... I don't pass anything to the compiler (at least that I would be aware of).

edit: just to make it clear. Everything is as default as it can be. I downloaded the latest version from their website and started coding this program. It's my first c++ program.

OK, then try to figure out how to pass the switches to the compiler. I don't have Dev-C++, but generally these options are found via something like;
Project / Settings / Compiler / Compiler Options. Can you find anything similar in Dev-C++?

Here is a link to the GCC Options to Request or Suppress Warnings
As you can see, there are quite a lot of them, the link is to gcc v.3.3.6 online docs (that might be close to the version that you have). At the very minimum, you want to pass the -Wall option to the compiler (enables all warnings).

[EDIT]
You can find out your compiler version by issueing gcc --version in a command prompt.

Edited 6 Years Ago by mitrmkar: n/a

Thank you very much! I found the mistake thanks to it :-) I forgot to make SprawdzCzyPrzeniesc virtual, so it probably called it from the superclass. Is it possible that calling the non-virtual method on the object of subclass is sometimes done via one definition and sometimes via the other? The method in the superclass was empty, didn't even return anything. I didn't know that it in fact returns something nevertheless. It seems that it returns just anything.
Anyway, it seems to work fine now. So once again thank you for your time, I wouldn't probably be able to work it out alone.

>> Is it possible that calling the non-virtual method on the object of subclass is sometimes done via one definition and sometimes via the other?
As far as I understand the question, I would say no.

>> The method in the superclass was empty, didn't even return anything.
That's bad, when a method signature says it returns something, you must make it return something (that matches the signature).

By the way, here are more options GCC options Options Controlling C++ Dialect

And if you browse the GCC online docs site, you'll find even more. So, try figuring out what good they do and use them as you see fit making good use of the compiler. And make sure you are looking at the documentation that matches your compiler version.

This article has been dead for over six months. Start a new discussion instead.