Topic: Using NULL with cout.....a bug ??
Author: govind@miller.cs.uwm.edu (Govind Seshadri)
Date: Thu, 13 Feb 1992 18:29:05 GMT Raw View
Is it valid to use the NULL constant with cout ?
Ex: cout << NULL ;
I have seen that this has the same effect as
cout << "" ;
on all the C++ compilers I have used.
Or could this be a *bug* in these compilers ??
Thanx.
Govind
--
_____ =+=+=+=+=+May the Source be with you!=+=+=+=+=+=+=+=+=+=+=+=+=+
/ ____ . . . . . ___ AT&T : (414) 229 - 5980
/ __ / / . / / / \ / / ) INTERNET : govind@point.cs.uwm.edu
/__/ / /___/ ./ / / \/ /___/ "Kilroy was where ??!!"
Author: cline@sun.soe.clarkson.edu (Marshall Cline)
Date: Fri, 14 Feb 1992 08:11:38 GMT Raw View
In article <1992Feb13.182905.24251@uwm.edu> govind@miller.cs.uwm.edu (Govind Seshadri) writes:
>Is it valid to use the NULL constant with cout ?
> Ex: cout << NULL ;
The symbol `NULL' is not well defined in the manner you wish to use it.
The only `sure' way to check a pointer against the null pointer is to
check it against `0' (the FAQ has more to say about this issue). So,
if NULL is #define'd to be the token `0', cout will think it is printing
the integer zero, and will print "0". If NULL is #defined to be ((void*)0),
ostream::operator<<(const void*) will get control which prints out the
pointer as an address, usually in hex, and "0x00000000" or "0" will be
printed. If NULL is #defined as ((char*)0), or if there is no overload
for printing void*'s via ostream, cout may try to print this as a '\0'
terminated character string, and mayhem will break out (most Un*x boxes
will core dump for a segmentation violation; dos boxes will print the
interrupt vector table until it finds a zero byte, etc).
Consult the FAQ for more information on using (or rather, `not' using) the
#define'd symbol `NULL'.
--
Marshall Cline
--
Marshall P. Cline, Ph.D. / Paradigm Shift, Inc / 65 N Main St/Norwood, NY 13668
cline@sun.soe.clarkson.edu / 315-353-4585 / FAX: 315-353-6100
Author: guido@cwi.nl (Guido van Rossum)
Date: 14 Feb 92 10:44:47 GMT Raw View
govind@miller.cs.uwm.edu (Govind Seshadri) writes:
>Is it valid to use the NULL constant with cout ?
> Ex: cout << NULL ;
>I have seen that this has the same effect as
> cout << "" ;
>on all the C++ compilers I have used.
>
>Or could this be a *bug* in these compilers ??
I guess you shouldn't do this, since it is undefined what happens.
On my system NULL is defined (in iostream.h or in something it
includes) as just 0 and cout << NULL is equivalent to cout << 0 (and
this to cout << "0"). If your system defines NULL as (char*)0 (which
would be wrong for ANSI C but maybe your compiler is old) then the <<
operator for strings is called and if location 0 in your address space
contains a readable null byte it might print the empty string.
In ANSI C it is legal to define NULL as (void*)0, and if this is legal
in C++ too, it should print something like 0x0.
Finally note that different "standard" header files may contain
different definitions of NULL, and many user-supplied library files
*also* define it (if not already defined), so it may be a little hard
to find out how NULL is defined in your case, unless your compiler has
the equivalent of "CC -E" (show source after preprocessing).
--Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
"One's never alone with a rubber duck."
Author: steve@taumet.com (Stephen D. Clamage)
Date: 14 Feb 92 18:50:28 GMT Raw View
govind@miller.cs.uwm.edu (Govind Seshadri) writes:
>Is it valid to use the NULL constant with cout ?
> Ex: cout << NULL ;
>I have seen that this has the same effect as
> cout << "" ;
>on all the C++ compilers I have used.
I can't believe this is the case.
NULL is usually defined to be a literal 0 in C++. The expression
cout << NULL
then becomes
cout << 0
This calls ostream::operator<<(int), which should print the character zero.
It is possible, but unlikely, that NULL is defined as (void*)0,
as it usually is in Standard C. In that case the expression calls
ostream::operator<<(void*), which should print out whatever a null
pointer looks like, typically "0x0".
Finally, the expression
cout << ""
must call ostream::operator<<(const char*), which prints nothing,
since the string is empty.
--
Steve Clamage, TauMetric Corp, steve@taumet.com