From -1392010891048495159
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,53d56717e82ab663
X-Google-Attributes: gidf78e5,public
From: Ron Natalie <ron@sensor.com>
Subject: Re: cout << null pointers
Date: 2000/07/09
Message-ID: <3966405B.3BF79F21@sensor.com>#1/1
X-Deja-AN: 644038998
Content-Transfer-Encoding: 7bit
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <39656D42.E049FCAA@ix.netcom.com>
X-Accept-Language: en
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
Content-Type: text/plain; charset=x-user-defined
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 963088253 mail2news:16953 mail2news mail2news.demon.co.uk
Organization: Sensor Systems
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Mime-Version: 1.0
Newsgroups: comp.std.c++



Paul Meyerholtz wrote:
> 
> I have found an inconsistent output by various compilers of the following:
> 
> char * p = 0;
> cout * q = "";
> 
> 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 don't think VC++ failes the test.  It's pretty basic stuff.  It certainly
works fine in VC++ 5.
> 
> However, the handling of "cout << p" seems to cause problems for most
> computers, with results from segment faults to garbage output.

Not surprising, you are invoking undefined behavior.

> Why is the output of p and q different?

There is a whole lot of difference between a null pointer constant (0)
and an empty string literal.  One is a guaranteed invalid pointer, the
other is a pointer to a statically allocated array of one character with
a zero in it.

> 
> Is there anything in the standard about this?  Should cout check for the
> null pointer?

No, the programmer should avoid giving null pointers to things that are expecting
valid poitners.

---
[ 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              ]




