Topic: About Null references


Author: Christopher Eltschka <celtschk@web.de>
Date: Sat, 22 Dec 2001 03:17:23 CST
Raw View
"Umesh Nair" <umesh_nair@maps.mentor.spam.com> writes:

> Hi,
>
> I have seen code like the following:
>
> // some_file.h
>
> void foo (int x, ostream& os = *(ostream *)0);
>
>
> // some_file.cxx
>
> void foo (int x, ostream& os)
> {
>    ostream* out_p;
>    if (&os == (ostream*)0) {
>       out_p = &cout;
>    } else {
>       out_p = &os;
>    }
>    ...
>    ...
> }
>
> I know this is ugly. My question is whether this is legal. This is an
> attempt to use null references, just like null pointers, to denote error or
> default values. The code above is compiled on many popular compilers.

Did you also test if it does what you expect it to do on all those
compilers? At all optimization levels?

Dereferencing a null pointer is undefined behaviour, and there is no
way to initialize a reference to a "null value" without doing this.
Therefore the compiler is allowed to assume that the address-of
operator applied to a reference always yields a non-zero result.

That is, a compiler would be allowed to compile the function body as

{
  ostream* out_p = &os;
  ...
}

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Umesh Nair" <umesh_nair@maps.mentor.spam.com>
Date: Thu, 25 Oct 2001 05:52:26 CST
Raw View
Hi,

I have seen code like the following:

// some_file.h

void foo (int x, ostream& os = *(ostream *)0);


// some_file.cxx

void foo (int x, ostream& os)
{
   ostream* out_p;
   if (&os == (ostream*)0) {
      out_p = &cout;
   } else {
      out_p = &os;
   }
   ...
   ...
}

I know this is ugly. My question is whether this is legal. This is an
attempt to use null references, just like null pointers, to denote error or
default values. The code above is compiled on many popular compilers.

Thanks,


---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: James Dennett <jdennett@acm.org>
Date: Thu, 25 Oct 2001 16:38:41 GMT
Raw View
Umesh Nair wrote:

> Hi,
>
> I have seen code like the following:
>
> // some_file.h
>
> void foo (int x, ostream& os = *(ostream *)0);
>
>
> // some_file.cxx
>
> void foo (int x, ostream& os)
> {
>    ostream* out_p;
>    if (&os == (ostream*)0) {
>       out_p = &cout;
>    } else {
>       out_p = &os;
>    }
>    ...
>    ...
> }
>
> I know this is ugly. My question is whether this is legal. This is an
> attempt to use null references, just like null pointers, to denote error or
> default values. The code above is compiled on many popular compilers.

It's not legal.  *(ostream*)0 dereferences a null pointer,
and that is never legal in an expression which is evaluated.

-- James Dennett

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 25 Oct 2001 18:09:20 GMT
Raw View
In article <3bd7a8e2@solnews.wv.mentorg.com>, Umesh Nair <umesh_nair@map
s.mentor.spam.com> writes
>I have seen code like the following:
>
>// some_file.h
>
>void foo (int x, ostream& os = *(ostream *)0);
>
>
>// some_file.cxx
>
>void foo (int x, ostream& os)
>{
>   ostream* out_p;
>   if (&os == (ostream*)0) {
>      out_p = &cout;
>   } else {
>      out_p = &os;
>   }
>   ...
>   ...
>}
>
>I know this is ugly. My question is whether this is legal. This is an
>attempt to use null references, just like null pointers, to denote error or
>default values. The code above is compiled on many popular compilers.

I would expect compilers to compile that code, and I would expect
competent code reviewers to reject it because even if the code is legal
as it has been specifically written, it is a strong attractant for
undefined behaviour. I think any time you are tempted to do something
like that, something like this will achieve the same end, better, much
better:

void foo(int x, ostream & = cout);


Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Wil Bloodworth" <whbloodworth@nowhere.com>
Date: Thu, 25 Oct 2001 18:14:22 GMT
Raw View
Umesh,

It may compile on many compilers but it is not portable... nor advisable.

- W. Bloodworth

"Umesh Nair" <umesh_nair@maps.mentor.spam.com> wrote in message
news:3bd7a8e2@solnews.wv.mentorg.com...
> I have seen code like the following:

> void foo (int x, ostream& os = *(ostream *)0);
>
> void foo (int x, ostream& os)
> {
>    ostream* out_p;
>    if (&os == (ostream*)0) {
>       out_p = &cout;
>    } else {
>       out_p = &os;
>    }
>    ...
> }
>
> I know this is ugly. My question is whether this is legal. This is an
> attempt to use null references, just like null pointers, to denote error or
> default values. The code above is compiled on many popular compilers.


---
[ 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://www.research.att.com/~austern/csc/faq.html                ]