From -856349470529003359
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f78e5,53d56717e82ab663
X-Google-Attributes: gidf78e5,public
From: kanze@gabi-soft.de
Subject: Re: cout << null pointers
Date: 2000/07/09
Message-ID: <86puop7l06.fsf@gabi-soft.de>#1/1
X-Deja-AN: 643939049
Content-Transfer-Encoding: quoted-printable
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <39656D42.E049FCAA@ix.netcom.com>
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
Content-Type: text/plain; charset=iso-8859-1
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 963065853 mail2news:1957 mail2news mail2news.demon.co.uk
Organization: Customer of UUNET Deutschland GmbH
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
User-Agent: Gnus/5.0805 (Gnus v5.8.5) Emacs/20.4
MIME-Version: 1.0
X-MIME-Autoconverted: from 8bit to quoted-printable by mulga.cs.mu.OZ.AU id AAA00322
Newsgroups: comp.std.c++

Paul Meyerholtz <pholtz@ix.netcom.com> writes:

|>  I have found an inconsistent output by various compilers of the
|>  following:

|>  char * p =3D 0;
|>  cout * q =3D "";

|>  cout << p << "\n\n";
|>  cout << q;

|>  Most compilers seem to handle "cout << q" without a problem.  (But I
|>  have heard that Visual C++ 6 fails this test).

I'd be surprised.  I don't think any compiler would have a problem with
cout << q.

|>  However, the handling of "cout << p" seems to cause problems for
|>  most computers, with results from segment faults to garbage output.

Outputting a char* involves derefencing it.  Dereferening a null pointer
is undefined behavior.  Consider yourself lucky that it didn't reformat
your disk.

|>  Why is the output of p and q different?

Because the input is different.  Why should it be the same?  Why should
outputting a null pointer have any defined behavior?

|>  Is there anything in the standard about this?

That a program which attempts to output a null char* has undefined
behavior.  It is a programming error.

|>  Should cout check for the null pointer?

Why?  It could possibly check for a null pointer, but then, what about
all of the other illegal values?

--=20
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




