C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

write bool to ostream
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Mehturt@gmail.com
Guest





PostPosted: Wed Oct 19, 2005 8:50 am    Post subject: write bool to ostream Reply with quote



Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.

Thanks..
m.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stefan Näwe
Guest





PostPosted: Thu Oct 20, 2005 3:51 am    Post subject: Re: write bool to ostream Reply with quote



[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:

std::ofstream of;
bool b;

bool b=true; // always initialize.

Quote:
// ofstream open(), etc.
of<<"b="<

of << "b=" << (b?"true":"false") << 'n';


--
Stefan Naewe
naewe.s_AT_atlas_DOT_de

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Oct 20, 2005 10:35 am    Post subject: Re: write bool to ostream Reply with quote



[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<
to print anything else than 0 or 1?

Yes, accessing the value of a uninitialised variable yields undefined
behaviour.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Allan W
Guest





PostPosted: Thu Oct 20, 2005 11:15 am    Post subject: Re: write bool to ostream Reply with quote

[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:
std::ofstream of;
bool b;
// ofstream open(), etc.
of << "b=" << b << 'n';

to print anything else than 0 or 1?

Yes, sure.

Quote:
I am getting random integer numbers; using gcc 4.0.2.

You are invoking UB (Undefined Behavior) by writing a value
that was not initialized. Whatever happened to be on the stack
location occupied by b, is being displayed.

The UB *might* be more predictable if you do something like this:

bool b = false;
*(reinterpret_cast std::cout << "b=" << b << std::endl;

On my system this consistently writes the value 2... there's
nothing in the standard that guarantees this.

Initialize b to false, and see if you stop getting "Random
integer numbers".


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Markus Moll
Guest





PostPosted: Thu Oct 20, 2005 11:15 am    Post subject: Re: write bool to ostream Reply with quote

Hi

[email]Mehturt (AT) gmail (DOT) com[/email] wrote:

Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.

Yes, as the behaviour is undefined.

Markus

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Daniel Krügler
Guest





PostPosted: Thu Oct 20, 2005 11:19 am    Post subject: Re: write bool to ostream Reply with quote

[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.

This snipped is an example of a program causing
undefined behaviour (UB). Reason is: You are trying to
read a non-initialized variable (b).
The consequences of UB are neither guaranteed to be
documentable nor to be reproducible. One of the pictorial
examples is that a compliant compiler is allowed to
create code, which formats your harddisc or writes an
email to your boss... So: Don't do that!

Btw.: Although the standard allows practically everything
as a possible result of UB, it mentiones your example in a footnote:

"Using a bool value in ways described by this International Standard as
‘‘undefined,’’ such as by examining the value of an uninitialized
automatic variable, might cause it to behave as if it is neither true
nor false."

After setting an initial value to b, the output should be either
0 or 1 depending on the actual value of b.

Greetings from Bremen,

Daniel Krügler

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Sebastian Redl
Guest





PostPosted: Thu Oct 20, 2005 11:20 am    Post subject: Re: write bool to ostream Reply with quote

[email]Mehturt (AT) gmail (DOT) com[/email] wrote:

Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.


You didn't initialize your boolean. Reading it results in undefined
behaviour - in your case, in the bool having a value that shouldn't be
possible.

--
Sebastian Redl

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Nicola Musatti
Guest





PostPosted: Thu Oct 20, 2005 11:22 am    Post subject: Re: write bool to ostream Reply with quote


[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.

Yes, because b is not initialized. See the standard, 3.9.1-6, where
note 42 says: "Using a bool value in ways described by this
International Standard as ''undefined,'' such as by examining
the value of an uninitialized
automatic variable, might cause it to behave as if is neither true nor
false."

Cheers,
Nicola Musatti


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Thu Oct 20, 2005 11:23 am    Post subject: Re: write bool to ostream Reply with quote

<Mehturt (AT) gmail (DOT) com> wrote

Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.

Because you are invoking undefined behavior. Your bool is not initialized. As to
printing, you can print anything you want:
of << (b ? "good day" : "the sky is falling");

-- Gene
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ferdi Smit
Guest





PostPosted: Thu Oct 20, 2005 11:24 am    Post subject: Re: write bool to ostream Reply with quote

[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?
I am getting random integer numbers; using gcc 4.0.2.

You can use the std::boolalpha manipulator. Ie.

std::cout << std::boolalpha << (1 == 0) << std::endl;

will print "false".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Sumit Rajan
Guest





PostPosted: Thu Oct 20, 2005 11:25 am    Post subject: Re: write bool to ostream Reply with quote


<Mehturt (AT) gmail (DOT) com> wrote

Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?

Sorry, I did not mean to say "Yes" in the previous post in this thread.
Please disregard it.

Thanks.
Sumit.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Maeder
Guest





PostPosted: Thu Oct 20, 2005 11:26 am    Post subject: Re: write bool to ostream Reply with quote

"Mehturt (AT) gmail (DOT) com" <Mehturt (AT) gmail (DOT) com> writes:

Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

This reads an uninitialized variable. The program has thus undefined
behavior. Anything can happen, including a program crash and the
computer catching fire.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Sumit Rajan
Guest





PostPosted: Thu Oct 20, 2005 11:26 am    Post subject: Re: write bool to ostream Reply with quote


<Mehturt (AT) gmail (DOT) com> wrote

Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<

to print anything else than 0 or 1?

Yes.

Quote:
I am getting random integer numbers; using gcc 4.0.2.

Possibly because you have not initialized the variable.

Regards,
Sumit.
--
Sumit Rajan


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Mehturt@gmail.com
Guest





PostPosted: Thu Oct 20, 2005 11:26 am    Post subject: Re: write bool to ostream Reply with quote

Ok, I have found the problem.. I thought I have initialized the bool
variable in a function called before the output to ostream, but I did
not pass by reference as I intended to.. Therefore the bool variable
was uninitialized..


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Kanze
Guest





PostPosted: Thu Oct 20, 2005 12:22 pm    Post subject: Re: write bool to ostream Reply with quote

[email]Mehturt (AT) gmail (DOT) com[/email] wrote:
Quote:
Is it possible for:

std::ofstream of;
bool b;
// ofstream open(), etc.
of<<"b="<
to print anything else than 0 or 1?

It's possible for it to do just about anything, including format
your hard disk. Reading an uninitialized variable is undefined
behavior, and all bets are off.

Quote:
I am getting random integer numbers; using gcc 4.0.2.

Technically, they're within their rights.

From a quality of implementation point of view: on one hand, I'm
inclined to say that you should just get 0 or 1, regardless. On
the other, the problem only occurs if you have an error in your
code, and the earlier you detect the error, the better. Perhaps
the best solution would be an assertion failure, with what g++
is doing second best.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.