From 5501594677066145397
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,53d56717e82ab663
X-Google-Attributes: gidf78e5,public
From: Jack Klein <jackklein@spamcop.net>
Subject: Re: cout << null pointers
Date: 2000/07/09
Message-ID: <GJ5mOfT+wBGJX4IoDzMqyKZK21st@4ax.com>#1/1
X-Deja-AN: 643937700
Content-Transfer-Encoding: 7bit
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=us-ascii
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Trace: mail2news.demon.co.uk 963065525 mail2news:1722 mail2news mail2news.demon.co.uk
Organization: AT&T Worldnet
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
MIME-Version: 1.0
NNTP-Posting-Date: Sat, 08 Jul 2000 03:27:00 GMT
Newsgroups: comp.std.c++

On Sat, 8 Jul 2000 03:08:48 CST, Paul Meyerholtz
<pholtz@ix.netcom.com> wrote in comp.std.c++:

> I have found an inconsistent output by various compilers of the following:
> 
> char * p = 0;

This initialization makes p a null pointer.  It does not point to
anything and dereferencing it is undefined behavior.

> cout * q = "";

This creates an unnamed array of one character, containing the
character value '\0', and points q at that character.  It is in a
legal memory location and represents a C string of zero length.

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

Passing a pointer to char to cout causes it to dereference the pointer
to access all of the characters in sequence up to, and excluding, the
terminating '\0'.  Since p is a null pointer, dereferencing it
produces undefined behavior.

> cout << q;

This causes q to be dereferenced, resulting in the char '\0'.  Since
that is the terminating character, nothing is done.

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

I seriously doubt that it does.  It should merely output nothing.

> However, the handling of "cout << p" seems to cause problems for most
> computers, with results from segment faults to garbage output. 
> (CodeWarrior for the Mac, which I use, outputs a couple of garbage
> characters, but always the same ones).
> 
> Why is the output of p and q different?

They are not the same thing.  p points to nothing, q points to a 0
length C string, which is a perfectly legal object.

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

No, that's up to the programmer.  Passing a null pointer to any
function member or otherwise that is not specifically defined as
accepting a null pointer is a bug, and produces undefined behavior.

> Paul

Jack Klein
-- 
Home: http://jackklein.home.att.net

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




