Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: wording: "references cannot be null"

50 views
Skip to first unread message
Message has been deleted

Christopher Pisz

unread,
May 5, 2015, 11:37:23 AM5/5/15
to
On 5/5/2015 10:24 AM, Stefan Ram wrote:
> When asked for the difference between pointers and references
> some say that references cannot be null. I know what this is
> /intended/ to mean, but the wording might not correctly convey
> this for readers who do not yet known this. For an example how
> this can be (mis)understood, I show the following program that
> has a reference that /is/ null!
>
> #include <iostream>
> #include <ostream>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << q->i << '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> »q« is a reference, and it /is/ null!
>


No, q is a reference to a pointer that exists, and the pointer is null.


--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

Alain Ketterlin

unread,
May 5, 2015, 11:46:16 AM5/5/15
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> When asked for the difference between pointers and references
> some say that references cannot be null. I know what this is
> /intended/ to mean, but the wording might not correctly convey
> this for readers who do not yet known this. For an example how
> this can be (mis)understood, I show the following program that
> has a reference that /is/ null!
>
> #include <iostream>
> #include <ostream>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << q->i << '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> »q« is a reference, and it /is/ null!

No. q is a reference to a pointer, and the pointer's value is nullptr.
I.e., q refers to something that exists in a valid state (the variable p
of main). In the body of f(), writing "q = ...any pointer to a V..." is
perfectly ok. What you do instead is: accessing the value pointed to by
the pointer referenced by q. The former part (accessing the value
pointed to...) is faulty, the latter (the pointer referenced by q) is
valid.

You can still create an invalid reference:

V * p = nullptr;
V & q = *p;

but imho the initialization has undefined behavior because you
dereference an invalid pointer (even though you do not really
dereference it).

-- Alain.

Mel

unread,
May 5, 2015, 12:07:48 PM5/5/15
to
On 5 May 2015 15:24:45 GMT, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> When asked for the difference between pointers and references
> some say that references cannot be null. I know what this is
> /intended/ to mean, but the wording might not correctly convey
> this for readers who do not yet known this. For an example how
> this can be (mis)understood, I show the following program that
> has a reference that /is/ null!


> #include <iostream>
> #include <ostream>


> struct V { int i; };


> void f( V * & q ){ ::std::cout << q->i << '\n'; }


> int main(){ V * p = nullptr; f( p ); }


> »q« is a reference, and it /is/ null!

Nope, q is not null, rather pointer which it refers...

--
Press any key to continue or any other to quit
Message has been deleted

Melzzzzz

unread,
May 5, 2015, 12:55:53 PM5/5/15
to
On 5 May 2015 16:26:57 GMT
r...@zedat.fu-berlin.de (Stefan Ram) wrote:

> Mel <m...@zzzzz.com> writes:
> >Nope, q is not null, rather pointer which it refers...
>
> The following prints »true«.
>
> #include <iostream>
> #include <ostream>
> #include <ios>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << ::std::boolalpha <<( q == 0 )<<
> '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> Saying, »q is not null«, when in fact »q == 0« /is/ true, is
> something that can be understood by all those who can guess what the
> intended meaning is, but might not be true by a strict reading of the
> wording.
>

You don't understand at all what references are about.
You could write void f(V *q) and get same result....

red floyd

unread,
May 5, 2015, 2:51:17 PM5/5/15
to
On 5/5/2015 8:24 AM, Stefan Ram wrote:
> When asked for the difference between pointers and references
> some say that references cannot be null. I know what this is
> /intended/ to mean, but the wording might not correctly convey
> this for readers who do not yet known this. For an example how
> this can be (mis)understood, I show the following program that
> has a reference that /is/ null!
>
> #include <iostream>
> #include <ostream>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << q->i << '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> »q« is a reference, and it /is/ null!
>

Perhaps a better way of phrasing it is thus:

Reference: the referenced object always exists (modulo UB, of course)
Pointer: the object pointed to may or may not (nullptr) exist
(again, modulo UB)

Alain Ketterlin

unread,
May 5, 2015, 3:41:34 PM5/5/15
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Mel <m...@zzzzz.com> writes:
>>Nope, q is not null, rather pointer which it refers...
>
> The following prints »true«.
>
> #include <iostream>
> #include <ostream>
> #include <ios>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << ::std::boolalpha <<( q == 0 )<< '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> Saying, »q is not null«, when in fact »q == 0« /is/ true,

The reference q is not null, i.e., it actually references a valid
object.

In "q == 0" you are not comparing the reference to 0, you are comparing
the referenced oject (a pointer) to 0.

(BTW: you should use nullptr instead of 0, were it only for
consistency.)

> is something that can be understood by all those who can guess what
> the intended meaning is, but might not be true by a strict reading
> of the wording.

I can't parse this.

-- Alain.

Tobias Müller

unread,
May 5, 2015, 3:58:59 PM5/5/15
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> When asked for the difference between pointers and references
> some say that references cannot be null. I know what this is
> /intended/ to mean, but the wording might not correctly convey
> this for readers who do not yet known this. For an example how
> this can be (mis)understood, I show the following program that
> has a reference that /is/ null!
>
> #include <iostream>
> #include <ostream>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << q->i << '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> »q« is a reference, and it /is/ null!

The statement that a reference can (or cannot) be null makes no sense to
begin with.
A reference is an abstract concept. It's not a variable and most important
it is/has no value. It refers _refers_ to a variable, which can have a
value, but not the reference by itself.

Tobi

Juha Nieminen

unread,
May 6, 2015, 4:46:03 AM5/6/15
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> The following prints »true«.
>
> #include <iostream>
> #include <ostream>
> #include <ios>
>
> struct V { int i; };
>
> void f( V * & q ){ ::std::cout << ::std::boolalpha <<( q == 0 )<< '\n'; }
>
> int main(){ V * p = nullptr; f( p ); }
>
> Saying, »q is not null«, when in fact »q == 0« /is/ true, is something
> that can be understood by all those who can guess what the intended
> meaning is, but might not be true by a strict reading of the wording.

That's like saying that an int& is "null" if the int it references
happens to have the value 0.

The reference is not null. The value it points to might be.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Drew Lawson

unread,
May 6, 2015, 2:27:19 PM5/6/15
to
In article <references-2...@ram.dialup.fu-berlin.de>
r...@zedat.fu-berlin.de (Stefan Ram) writes:
> When asked for the difference between pointers and references
> some say that references cannot be null.

If you say so. What normally hear is some variation of "references
must be bound to an object."


--
Drew Lawson | It's not enough to be alive
| when your future's been deferred

Richard

unread,
May 6, 2015, 2:55:53 PM5/6/15
to
[Please do not mail me a copy of your followup]

dr...@furrfu.invalid (Drew Lawson) spake the secret code
<midmds$7i1$1...@raid.furrfu.com> thusly:

>In article <references-2...@ram.dialup.fu-berlin.de>
> r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> When asked for the difference between pointers and references
>> some say that references cannot be null.
>
>If you say so. What normally hear is some variation of "references
>must be bound to an object."

Every time I've asked the question in an interview, noone knew the
answer. Rather than being pedantic about the form of their answer, I
would simply accept anything that indicated that they knew that you
don't need to check references to see if they refer to something.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Melzzzzz

unread,
May 6, 2015, 3:18:46 PM5/6/15
to
On Wed, 6 May 2015 18:55:42 +0000 (UTC)
legaliz...@mail.xmission.com (Richard) wrote:

> [Please do not mail me a copy of your followup]
>
> dr...@furrfu.invalid (Drew Lawson) spake the secret code
> <midmds$7i1$1...@raid.furrfu.com> thusly:
>
> >In article <references-2...@ram.dialup.fu-berlin.de>
> > r...@zedat.fu-berlin.de (Stefan Ram) writes:
> >> When asked for the difference between pointers and references
> >> some say that references cannot be null.
> >
> >If you say so. What normally hear is some variation of "references
> >must be bound to an object."
>
> Every time I've asked the question in an interview, noone knew the
> answer. Rather than being pedantic about the form of their answer, I
> would simply accept anything that indicated that they knew that you
> don't need to check references to see if they refer to something.

How would you check reference too see if they refer to something?

Victor Bazarov

unread,
May 6, 2015, 4:29:43 PM5/6/15
to
In good ol' times, when everybody simply ignored UB, checking a null
reference was done by comparing the address of the object with 0. Same
with 'this' ('coz sometimes it just was "necessary to call a member
function for a non-existent object"):

void foo(sometype &ref) {
if (&ref != NULL) ...
}

or

void sometype::foo() {
if (this != NULL) ...

How else? :-)

Of course, the biggest problem was uninitialized pointers (not null
ones) which in the worst case would point to some random place in valid
memory, and references that were initialized by indirection from such
pointers.

V
--
I do not respond to top-posted replies, please don't ask

Richard

unread,
May 6, 2015, 5:26:06 PM5/6/15
to
[Please do not mail me a copy of your followup]

Melzzzzz <m...@zzzzz.com> spake the secret code
<20150506211836.0dd2db43@maxa-pc> thusly:
You can't. That is what indicates that they understand the difference
between a reference and a pointer. If they think references are just
pointers with . syntax instead of -> syntax, it means they don't
understand references.
0 new messages