13
int&  fun()
{
    int * temp = NULL;
    return *temp;
}

In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give exception if it is by value then it does. Even when dereferencing of NULL pointer is assinged to reference (like the below line) then also it does not give.

int* temp = NULL:
int& temp1 = *temp;

Here my question is that does not compiler do the dereferencing in case of reference?

1
  • 4
    References are handled as pointers internally they are just differently in the syntax you use on them. Knowing that your dereferenciation only "assigns" the pointervalue to the reference making it a reference to NULL. That does not trigger any memory acces. When you return by value the dereferenciation will result in a memory access at 0 which almost always gives you a segfault. Commented Jul 16, 2011 at 8:31

5 Answers 5

17

Dereferencing a null pointer is Undefined Behavior.

An Undefined Behavior means anything can happen, So it is not possible to define a behavior for this.

Admittedly, I am going to add this C++ standard quote for the nth time, but seems it needs to be.

Regarding Undefined Behavior,

C++ Standard section 1.3.24 states:

Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

NOTE:
Also, just to bring it to your notice:
Using a returned reference or pointer to a local variable inside a function is also an Undefined Behavior. You should be allocating the pointer on freestore(heap) using new and then returning a reference/pointer to it.

EDIT:
As @James McNellis, appropriately points out in the comments,
If the returned pointer or reference is not used, the behavior is well defined.

Sign up to request clarification or add additional context in comments.

10 Comments

Yes, agree on the above mentioned UB. but my question is if dereferencing of NULL pointer or non NULL pointer is done to assinged that to a reference then does compiler do the dereferencing operation? like- Int* t = NULL; Int& t1 = *t;
@G Mann: Reference is just an alias to the original type it was initialized to. How it is implemented is an implementation detail of compilers & standard does not define how it should be implemented.
When you deference a null pointer the code is invalid, and the compiler can do just anything. It is not meaningful to ask why it did anything.
@G Mann - Exactly what happens depends on how a reference is implemented. The standard doesn't say that either, just how a reference should work.
"Returning a reference or pointer to a local variable inside a function is also an Undefined Behavior." This is incorrect: if the returned pointer or reference is not used, the behavior is well defined.
|
7

When you dereference a null pointer, you don't necessarily get an exception; all that is guaranteed is that the behavior is undefined (which really means that there is no guarantee at all as to what the behavior is).

Once the *temp expression is evaluated, it is impossible to reason about the behavior of the program.

3 Comments

I am adding the same comment as i did in other post- Yes, agree on the above mentioned UB. but my question is if dereferencing of NULL pointer or non NULL pointer is done to assinged that to a reference then does compiler do the dereferencing operation? like- Int* t = NULL; Int& t1 = *t;
It is the case that for most compilers, in many circumstances, a reference is implemented as a pointer. Beyond that, it depends on the compiler, settings, etc.
Correct me i am wrong. Where it is implemented as pointer then compiler may not be doing the dereferencing operation.
4

You are not allowed to dereference a null pointer, so the compiler can generate code assuming that you don't do that. If you do it anyway, the compiler might be nice and tell you, but it doesn't have to. It's your part of the contract that says you must not do it.

In this case, I bet the compiler will be nice and tell you the problem already at compile time, if you just set the warning level properly.

Comments

1

Don't * a null pointer, it's UB. (undefined behavior, you can never assume it'll do anything short of lighting your dog on fire and forcing you to take shrooms which will lead to FABULOUS anecdotes)

Some history and information of null pointers in the Algol/C family: http://en.wikipedia.org/wiki/Pointer_(computing)#Null_pointer

Examples and implications of undefined behavior: http://en.wikipedia.org/wiki/Undefined_behavior#Examples_in_C

Comments

0

I don't sure I understand what you're trying todo. Dereferencing of ** NULL** pointer is not defined.

In case you want to indicate that you method not always returns value you can declare it as:

bool fun(int &val);

or stl way (similar to std::map insert):

std::pair<int, bool> fun();

or boost way:

boost::optional<int> fun();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.