Null Reference

J

JKop

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
 
P

Phlip

JKop said:
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?
 
S

Sharad Kala

JKop said:
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
 
C

Chris Theis

JKop said:
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
 
T

Tim Threlfall

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.
 
J

Jacek Dziedzic

JKop said:
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.
 
J

JKop

Jacek Dziedzic posted:
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
 
C

Chris Theis

JKop said:
Jacek Dziedzic posted:


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
 
J

JKop

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
 
C

Chris Theis

JKop said:
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
 
J

JKop

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
 
G

Gary Labowitz

Chris Theis said:
JKop said:
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!!
 
J

JKop

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
 
P

Phlip

JKop said:
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/flame_warriors/flame_67.php
 
J

JKop

Phlip posted:
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top