references as null

R

Rahul

Hi Everyone,

I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.

Thanks in advance!!!
 
T

Tomás Ó hÉilidhe

Rahul:
Hi Everyone,

I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.

Thanks in advance!!!


There is of course:

int *p = 0;

int &i = *p;


Only problem with this though is that the Standard leaves the behaviour
as explicitly undefined when you dereference a null pointer. So that
means don't do it in a portable program unless you want the hard disk
heads to crash and destroy all data on your disk :-D

And as far as I know, there's no other means of getting a "reference to
null" than dereferencing a null pointer (...I can't think of any off-hand
anyway).

Maybe your own system will let you do it, but it's not a habit you wanna
pick up if you want to program portably.
 
K

Kira Yamato

Hi Everyone,

I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.

I suppose you can try
Object &x = *(Object *)0;

And then you can test for NULL reference with
if (&x == 0) ...

But just because you could, should you?
 
R

Rahul

I suppose you can try
Object &x = *(Object *)0;

And then you can test for NULL reference with
if (&x == 0) ...

But just because you could, should you?


I just wanted to know the possibility of passing a NULL reference to a
copy constructor and as per your code i'm able to do so and vc++ is
crashing :)

class copu
{
int j;
public:
copu(const copu& obj)
{
printf("in copy constructor...%d\n",j);
j = obj.j; //-> crash over
here ;-)
printf("in copy constructor...2. %d\n",j);
}
copu()
{
j = 10;
printf("in default constructor...%d\n",j);
}

};

int main()
{
copu obj;
copu& ref = *(copu*)0;
copu sam = ref; //-> invokes the copy constructor
}
 
R

Rahul

I just wanted to know the possibility of passing a NULL reference to a
copy constructor and as per your code i'm able to do so and vc++ is
crashing :)

class copu
{
int j;
public:
copu(const copu& obj)
{
printf("in copy constructor...%d\n",j);
j = obj.j; //-> crash over
here ;-)
printf("in copy constructor...2. %d\n",j);
}
copu()
{
j = 10;
printf("in default constructor...%d\n",j);
}

};

int main()
{
copu obj;
copu& ref = *(copu*)0;
copu sam = ref; //-> invokes the copy constructor

}

So is there anyway to avoid referring to a variable of a NULL
reference? A developer of a class should consider this for a robust
class, he can't expect the user of the class to do the correct things.
I just want to have a graceful exit from the copy constructor...
 
Y

yanlinlin82

In my opinion, you may check it like this:

copu::copu(const copu& obj)
{
assert(&obj != NULL);
//...
j = obj.j
//...
}

But since the obj is invalid, the copy constructing can not be all
right. Aborting by assert is the best. :p
 
P

peter koch

So is there anyway to avoid referring to a variable of a NULL
reference? A developer of a class should consider this for a robust
class, he can't expect the user of the class to do the correct things.
I just want to have a graceful exit from the copy constructor...

The problem with the code above is that you invoke undefined behaviour
by dereferencing a null pointer. This code is not worth bothering
about (the program becomes invalid at that point), so there is no
reason and no need to check for this.

/Peter
 
R

Rolf Magnus

Rahul said:
So is there anyway to avoid referring to a variable of a NULL
reference?

Yes. Don't initialize a reference by dereferencing a null pointer. The place
where the error happens (and where the C++ standard says that the behavior
becomes undefined) is the place where you do that, not the place where you
use the invalid reference.
A developer of a class should consider this for a robust class, he can't
expect the user of the class to do the correct things.

He must. There is always a way to screw things up. It is more likely that
the user provides a reference to an object that is already destroyed. That
would be just as disastrous, and there is no way at all to check for that.
I just want to have a graceful exit from the copy constructor...

By doing what?
 
R

Ron Natalie

Rahul said:
Hi Everyone,

I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.

Thanks in advance!!!

No, there's no such thing as a null reference and anything
that coerces one almost certainly causes undefined behavior.

One thing you can do is use a reference to an object that you
can tell from a valid one:

class C {
public:
static C my_null;

bool IsNull(const C& t) {
return &t == &my_null;
}

void SomeFunc(const C& c = my_null) {
if(IsNull(c)) {...
 
J

James Kanze

I was wondering if there is any way to have a reference initialized
to NULL just like a pointer.

Not legally, unless your compiler supports it as an extension.
(I don't know of any that do.) It's undefined behavior;
anything can happen.
 

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
474,186
Messages
2,570,997
Members
47,586
Latest member
Gilda57E93

Latest Threads

Top