By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Null Reference

P: n/a

Does that Standard explicitly forbid the initiation of a null reference? Is
there anything wrong with the following code?:

void Blah( std::string const &k )
{
if ( !&k ) return;

// work with k
}

int main()
{
Blah( *static_cast<std::string* const>( 0 ) );
}

(Not sure if reinterpret_cast is required there...)

-JKop
Jul 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
JKop wrote:
Does that Standard explicitly forbid the initiation of a null reference? Is there anything wrong with the following code?:

void Blah( std::string const &k )
{
if ( !&k ) return;

// work with k
}

int main()
{
Blah( *static_cast<std::string* const>( 0 ) );


The undefined behavior begins with the first * on that line. Dereferencing a
pointer to NULL is undefined.

Does anyone know a compiler that won't generate the obvious opcodes for that
situation, and evaluate 'if ( !&k ) return;' the way anyone would expect?

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

P: n/a

"JKop" <NU**@NULL.NULL> wrote in message
Does that Standard explicitly forbid the initiation of a null reference? Is there anything wrong with the following code?:


Yes, section 8.3.2 paragraph 4

"A reference shall be initialized to refer to a valid object or function.
[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 dereferencing a null pointer, which causes
undefined behavior."

Sharad

Jul 22 '05 #3

P: n/a
You cannot have null references in a well defined program. The only way to
obtain a null reference would be to deference a null pointer(as in your
code), which results in undefined behaviour.
"JKop" <NU**@NULL.NULL> wrote in message
news:pH*******************@news.indigo.ie...

Does that Standard explicitly forbid the initiation of a null reference?
Is
there anything wrong with the following code?:

void Blah( std::string const &k )
{
if ( !&k ) return;

// work with k
}

int main()
{
Blah( *static_cast<std::string* const>( 0 ) );
}

(Not sure if reinterpret_cast is required there...)

-JKop

Jul 22 '05 #4

P: n/a

"JKop" <NU**@NULL.NULL> wrote in message
news:pH*******************@news.indigo.ie...

Does that Standard explicitly forbid the initiation of a null reference?

[SNIP]

Yes. Itīs in the nature of references that there is no null reference!

Chris
Jul 22 '05 #5

P: n/a
JKop wrote:
Does that Standard explicitly forbid the initiation of a null reference?


Forbid or not, the Standard does not give you means to generate one.

- J.
Jul 22 '05 #6

P: n/a
Jacek Dziedzic posted:
JKop wrote:
Does that Standard explicitly forbid the initiation of a null
reference?


Forbid or not, the Standard does not give you means to generate one.

- J.


struct Blah1
{
int& a;
};

struct Blah2
{
int* p_a;
};
#include <iostream>

int main()
{
Blah2 poo2 = { 0 };

Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );

int& nulref = poo1.a;

std::cout << "I'm a reference, and my address is: " << &nulref << '\n';
}

-JKop
Jul 22 '05 #7

P: n/a

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:no*******************@news.indigo.ie...
Jacek Dziedzic posted:
JKop wrote:
Does that Standard explicitly forbid the initiation of a null
reference?
Forbid or not, the Standard does not give you means to generate one.

- J.


struct Blah1
{
int& a;
};

struct Blah2
{
int* p_a;
};
#include <iostream>

int main()
{
Blah2 poo2 = { 0 };

Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );

int& nulref = poo1.a;

std::cout << "I'm a reference, and my address is: " << &nulref <<

'\n'; }

-JKop


And what exactly are you trying to proove with this? I mean we all know that
with the help of reinterpret_cast you can do a lot of (evil) things and it
is (or rather should be) common knowledge that not all syntactically legal
implementations result in well-defined programs. Your code does not show a
way to initialize a null reference but rather how to bind a reference to a
null pointer.

Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
A reference shall be initialized to refer to a valid object or function.
[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 dereferencing a null pointer,
which causes undefined behavior.

Regards
Chris
Jul 22 '05 #8

P: n/a
> Your code does not show a way to initialize a null reference but rather
how to bind a reference to a null pointer.
Incorrect.

My code depends upon the "Blah1" and the "Blah2" structures being identical.
I'm depending upon hidden pointers being used in the background for
references.

But... the Standard doesn't make any such guarantee that references will be
represented by hidden pointers at all! As such my code exhibits undefined
behaviour.

That said, I don't know of *any* system that wouldn't use hidden pointers
for my "Blah1" structure. So... it works on all the systems I know.

So when I make poo1 = poo2, what happens is that the hidden pointer in the
Blah1 structure gets the value 0, ie. a null pointer. By this I have
achieved a null reference, but without using the dereference operator.
Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
A reference shall be initialized to refer to a valid object or
function. [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 dereferencing a null
pointer, which causes undefined behavior.


I haven't dereferenced a null pointer.
-JKop
Jul 22 '05 #9

P: n/a

"JKop" <NU**@NULL.NULL> wrote in message
news:R1*******************@news.indigo.ie...
Your code does not show a way to initialize a null reference but rather
how to bind a reference to a null pointer.
Incorrect.

My code depends upon the "Blah1" and the "Blah2" structures being

identical. I'm depending upon hidden pointers being used in the background for
references.

But... the Standard doesn't make any such guarantee that references will be represented by hidden pointers at all! As such my code exhibits undefined
behaviour.

That said, I don't know of *any* system that wouldn't use hidden pointers
for my "Blah1" structure. So... it works on all the systems I know.

So when I make poo1 = poo2, what happens is that the hidden pointer in the
Blah1 structure gets the value 0, ie. a null pointer. By this I have
achieved a null reference, but without using the dereference operator.
Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
A reference shall be initialized to refer to a valid object or
function. [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 dereferencing a null
pointer, which causes undefined behavior.


I haven't dereferenced a null pointer.


After rechecking your code I saw that you really do not dereference a null
pointer but get around that by the (IMHO dubious) invocation of the copy
ctor & reinterpret_cast. However, you still do not acutally intialize a
null-reference but rather trick the compiler into it by this construct.

As an answer to your original question the standard says: "Note: in
particular, a null reference cannot exist in a
well-defined program.".

To make a long story short, what is your point of doing or wanting this? In
my experience itīs much more fruitful (in the monetary sense and also
thinking of ones career) to write well-defined programs ;-)
Cheers
Chris

Jul 22 '05 #10

P: n/a
After rechecking your code I saw that you really do not dereference a
null pointer but get around that by the (IMHO dubious) invocation of
the copy ctor & reinterpret_cast. However, you still do not acutally
intialize a null-reference but rather trick the compiler into it by
this construct.
The ends justifies the means.
As an answer to your original question the standard says: "Note: in
particular, a null reference cannot exist in a
well-defined program.".

To make a long story short, what is your point of doing or wanting
this? In my experience itīs much more fruitful (in the monetary sense
and also thinking of ones career) to write well-defined programs ;-)
Cheers
Chris

Is love well-defined? hehe

What ever happened to just writing a program that didn't exhibit undefined
behaviour? "defined programs" if you will!
-JKop
Jul 22 '05 #11

P: n/a
"Chris Theis" <Ch*********@nospam.cern.ch> wrote in message
news:cj**********@sunnews.cern.ch...

"JKop" <NU**@NULL.NULL> wrote in message
news:R1*******************@news.indigo.ie...
Your code does not show a way to initialize a null reference but rather how to bind a reference to a null pointer.
Incorrect.

My code depends upon the "Blah1" and the "Blah2" structures being

identical.
I'm depending upon hidden pointers being used in the background for
references.

But... the Standard doesn't make any such guarantee that references will

be
represented by hidden pointers at all! As such my code exhibits undefined behaviour.

That said, I don't know of *any* system that wouldn't use hidden pointers for my "Blah1" structure. So... it works on all the systems I know.

So when I make poo1 = poo2, what happens is that the hidden pointer in the Blah1 structure gets the value 0, ie. a null pointer. By this I have
achieved a null reference, but without using the dereference operator.
Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
A reference shall be initialized to refer to a valid object or
function. [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 dereferencing a null
pointer, which causes undefined behavior.


I haven't dereferenced a null pointer.


After rechecking your code I saw that you really do not dereference a null
pointer but get around that by the (IMHO dubious) invocation of the copy
ctor & reinterpret_cast. However, you still do not acutally intialize a
null-reference but rather trick the compiler into it by this construct.

As an answer to your original question the standard says: "Note: in
particular, a null reference cannot exist in a
well-defined program.".

To make a long story short, what is your point of doing or wanting this?

In my experience itīs much more fruitful (in the monetary sense and also
thinking of ones career) to write well-defined programs ;-)


Well, if the boss ever asks for a programmer that codes UB code that appears
to work but might fail, and enjoys doing it, we know who to refer!!
--
Gary
Jul 22 '05 #12

P: n/a
Well, if the boss ever asks for a programmer that codes UB code that
appears to work but might fail, and enjoys doing it, we know who to
refer!!

I take that as a compliment!
-JKop
Jul 22 '05 #13

P: n/a
JKop wrote:
struct Blah1
{
int& a;
};
This won't compile. If you fix it to compile, then it might dereference a
NULL pointer. But...
int main()
{
Blah2 poo2 = { 0 };

Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );


Undefined behavior would begin here, when you use the storage for poo2 as
something that it is not.

The remaining code is therefor undefined; it neither does nor does not
"dereference a NULL pointer".

(Great to have a target around to practice language law on, huh?)

http://www.politicsforum.org/images/...s/flame_67.php

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #14

P: n/a

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:7R*******************@news.indigo.ie...
Well, if the boss ever asks for a programmer that codes UB code that
appears to work but might fail, and enjoys doing it, we know who to
refer!!

I take that as a compliment!
-JKop


There you go ;-) I'll come back to that ;-)

Chris
Jul 22 '05 #15

P: n/a
Phlip posted:
JKop wrote:
struct Blah1
{
int& a;
};
This won't compile. If you fix it to compile, then it might dereference a
NULL pointer. But...

Incorrect to the former. "Wha... ?" to the latter.
int main()
{
Blah2 poo2 = { 0 };

Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );


Undefined behavior would begin here, when you use the storage for poo2 as
something that it is not.

I've already covered this.

The remaining code is therefor undefined; it neither does nor does not
"dereference a NULL pointer".

Here's a medal.
-JKop
Jul 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.