Topic: Initializing reference w/ null


Author: James.Kanze@dresdner-bank.com
Date: 2000/11/28
Raw View
In article <8vu76f$3rv$1@nnrp1.deja.com>,
  Alan Griffiths <alan.griffiths@uk.experian.com> wrote:
> In article <3A1D40AA.948C2227@evtechnology.com>,
>   James Dennett <james@evtechnology.com> wrote:

> > There are differences of opinion, related to the fact that using
> > NULL in C++ doesn't give you any technical advantages over using
> > 0 because NULL is not at all type-safe.

> > There are two advantages though:
> > (1) It documents the intent to refer to a null pointer constant,
> > and so
> > (2) It will be easier to update code if we ever get around to
> > making something like __null the only legal null pointer constant.

> > One disadvantage of using NULL is that very old compilers may have
> > shipped with a definition of NULL as (void*)0, which is legal for
> > C but illegal for C++.  Such a compiler is very broken as a C++
> > compiler, and would force people to abandon NULL.

> The principle disadvantage is that it documents an intent that isn't
> supported by the meaning of the code:

>     #include <iostream>

>     void f(int)   { std::cout << "f(int)\n"; }
>     void f(void*) { std::cout << "f(void*)\n"; }

>     int main() {
>         f(NULL); // Which f()?
>     }

True.  But a compiler can easily warn about using NULL as an int
here.  Mine does, anyway:

    null.cc: In function `int main()':
    null.cc:11: warning: converting NULL to non-pointer type

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


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: James Dennett <james@evtechnology.com>
Date: 2000/11/27
Raw View
Jens Lenge wrote:
>
> > No, dereferencing a NULL pointer causes undefined behaviour.
>
> By the way:
> Is it true that the NULL macro is intended to be no longer used,
> and the constant 0 should be used instead?
>
> I cannot remember exactly but I have something like that in mind...

No, it is not true.

There are differences of opinion, related to the fact that using
NULL in C++ doesn't give you any technical advantages over using
0 because NULL is not at all type-safe.

There are two advantages though:
(1) It documents the intent to refer to a null pointer constant,
and so
(2) It will be easier to update code if we ever get around to
making something like __null the only legal null pointer constant.

One disadvantage of using NULL is that very old compilers may
have shipped with a definition of NULL as (void*)0, which is legal
for C but illegal for C++.  Such a compiler is very broken as a
C++ compiler, and would force people to abandon NULL.

NULL is definitely not deprecated by the C++ Standard (ISO/IEC
14882:1998), and I personally prefer code not to look as if it
is assigning integers to pointers.  (I know that's not what's
happening, but int*p = (1-1) is just plain silly even if it is
legal.)

-- James Dennett <jdennett@acm.org>

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Alan Griffiths <alan.griffiths@uk.experian.com>
Date: 2000/11/27
Raw View
In article <3A1D40AA.948C2227@evtechnology.com>,
  James Dennett <james@evtechnology.com> wrote:
>
> There are differences of opinion, related to the fact that using
> NULL in C++ doesn't give you any technical advantages over using
> 0 because NULL is not at all type-safe.
>
> There are two advantages though:
> (1) It documents the intent to refer to a null pointer constant,
> and so
> (2) It will be easier to update code if we ever get around to
> making something like __null the only legal null pointer constant.
>
> One disadvantage of using NULL is that very old compilers may
> have shipped with a definition of NULL as (void*)0, which is legal
> for C but illegal for C++.  Such a compiler is very broken as a
> C++ compiler, and would force people to abandon NULL.

The principle disadvantage is that it documents an intent that isn't
supported by the meaning of the code:

    #include <iostream>

    void f(int)   { std::cout << "f(int)\n"; }
    void f(void*) { std::cout << "f(void*)\n"; }

    int main() {
        f(NULL); // Which f()?
    }
--
Alan Griffiths                        http://www.octopull.demon.co.uk/
Senior Systems Consultant, Experian   tel: +44 115 968 5118
Chair, Association of C and C++ Users http://accu.org/


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: James Dennett <james@evtechnology.com>
Date: 2000/11/27
Raw View
Alan Griffiths wrote:
>
> In article <3A1D40AA.948C2227@evtechnology.com>,
>   James Dennett <james@evtechnology.com> wrote:
> >
> > There are differences of opinion, related to the fact that using
> > NULL in C++ doesn't give you any technical advantages over using
> > 0 because NULL is not at all type-safe.
> >
> > There are two advantages though:
> > (1) It documents the intent to refer to a null pointer constant,
> > and so
> > (2) It will be easier to update code if we ever get around to
> > making something like __null the only legal null pointer constant.
> >
> > One disadvantage of using NULL is that very old compilers may
> > have shipped with a definition of NULL as (void*)0, which is legal
> > for C but illegal for C++.  Such a compiler is very broken as a
> > C++ compiler, and would force people to abandon NULL.
>
> The principle disadvantage is that it documents an intent that isn't
> supported by the meaning of the code:
>
>     #include <iostream>
>
>     void f(int)   { std::cout << "f(int)\n"; }
>     void f(void*) { std::cout << "f(void*)\n"; }
>
>     int main() {
>         f(NULL); // Which f()?
>     }

Two notes on that:
(1) It's usually bad form to overload on pointer types and integers,
largely for this reason.  (I.e., I agree with you, given the state of
C++ today.)
(2) Ideally, we'd change C++.  f(NULL) would be f(__null) where
__null is an instance of __null_type, and __null_type has only one
value, and converts implicitly to a null pointer of any given type.
That would change the answer to your question (the code should not
then call f(int)) and so might make people scream about a backwards
compatibility problem.  Ah well.  I'm evil so I'd like to punish
people for using NULL to mean an integral constant <g>.

-- 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: James Kuyper <kuyper@wizard.net>
Date: Tue, 28 Nov 2000 00:23:24 GMT
Raw View
Alan Griffiths wrote:
...
[Re: using NULL vs. using 0]
> The principle disadvantage is that it documents an intent that isn't
> supported by the meaning of the code:
>
>     #include <iostream>
>
>     void f(int)   { std::cout << "f(int)\n"; }
>     void f(void*) { std::cout << "f(void*)\n"; }
>
>     int main() {
>         f(NULL); // Which f()?
>     }

That code should be rewritten, either as f(0), or as f((void*)NULL),
depending upon the intent. The one thing it should not say is f(NULL).

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Greg Galloway <ggalloway625@my-deja.com>
Date: 2000/11/21
Raw View
I've recently come across some C++ code which initializes
references w/ null.  It works.  Its ugly.  But is this legal?

  class C;

  class T
  {
  public:
        ...

      C& getData(size_t i)
      {
          if (i < NDATA)
              return m_data[i];
          else
              return *((C*)0);   // return reference to null pointer
      }

        ...

  private:
      enum { NDATA = 10 };
      C m_data[NDATA];
  }

  bool foo(T& t, size_t i)
  {
     C& c = t.getData(i);
     if (!&c) return false;   // getData() failed, reference was null

       ...
  }

Thanks,
Greg


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Pete Becker <petebecker@acm.org>
Date: 2000/11/21
Raw View
Greg Galloway wrote:
>
> I've recently come across some C++ code which initializes
> references w/ null.  It works.  Its ugly.  But is this legal?
>

No.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Contributing Editor, C/C++ Users Journal (http://www.cuj.com)

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: James Dennett <james@evtechnology.com>
Date: 2000/11/21
Raw View
Greg Galloway wrote:
>
> I've recently come across some C++ code which initializes
> references w/ null.  It works.  Its ugly.  But is this legal?

No it is not.  It dereferences a null pointer, invoking undefined behaviour.

Tests for whether the address of a reference is null can be optimized out
of existence by a compiler; the address of a reference is the address of
the object to which it is bound, and a reference must always be bound to
an object.  No object has a null address.

The Standard addresses this in 8.3.2/4, which includes

  Note: in particular, a null reference cannot exist in a well-defined
  program, because the only way to create such a reference would be to
  bind it to the "object" obtained by derefencing a null pointer, which
  causes undefined behavior.

>   class C;
>
>   class T
>   {
>   public:
>         ...
>
>       C& getData(size_t i)
>       {
>           if (i < NDATA)
>               return m_data[i];
>           else
>               return *((C*)0);   // return reference to null pointer

That's undefined behaviour, derefencing an invalid pointer.

>       }
>
>         ...
>
>   private:
>       enum { NDATA = 10 };
>       C m_data[NDATA];
>   }
>
>   bool foo(T& t, size_t i)
>   {
>      C& c = t.getData(i);
>      if (!&c) return false;   // getData() failed, reference was null

The test can be optimised away.  (Because: any program which produces a
null reference must have invoked undefined behaviour, and if a program
invokes undefined behaviour then the compiler is free to do anything.
It follows that the compiler can always assume that undefined behaviour
has not been invoked, so that all references are bound to objects.)

There's been extensive debate on whether testing for null references
might be useful when writing libraries which must defend against
programmers who write code with undefined behaviour; this isn't the
forum for that debate.

>
>        ...
>   }

-- James Dennett <jdennett@acm.org>

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: Wed, 22 Nov 2000 01:57:22 GMT
Raw View
"Greg Galloway" <ggalloway625@my-deja.com> wrote...
> I've recently come across some C++ code which initializes
> references w/ null.  It works.  Its ugly.  But is this legal?

No, dereferencing a NULL pointer causes undefined behaviour.
We had a long thread about references last month.  Maybe you
could look it up...

>      return *((C*)0);   // return reference to null pointer
.             ^
.      Dereferencing

Victor
--
Please remove capital A's from my address when replying by mail



---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Jens Lenge <lenge@hft.e-technik.uni-dortmund.de>
Date: 2000/11/23
Raw View
> No, dereferencing a NULL pointer causes undefined behaviour.

By the way:
Is it true that the NULL macro is intended to be no longer used,
and the constant 0 should be used instead?

I cannot remember exactly but I have something like that in mind...

Jens


---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/11/23
Raw View
In article <8vjcim$hee$1@nx6.HRZ.Uni-Dortmund.DE>,
Jens Lenge  <lenge@hft.e-technik.uni-dortmund.de> wrote:
>> No, dereferencing a NULL pointer causes undefined behaviour.
>
>By the way:
>Is it true that the NULL macro is intended to be no longer used,
>and the constant 0 should be used instead?
>
>I cannot remember exactly but I have something like that in mind...

Check the discussions and discussions and discussions about
this on deja.com

- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ 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                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]