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?

Phil H
  • 19,928
  • 7
  • 68
  • 105
G Mann
  • 441
  • 1
  • 4
  • 12
  • 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. – Nobody moving away from SE Jul 16 '11 at 08:31

5 Answers5

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 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 Jul 16 '11 at 08:04
  • 1
    @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. – Alok Save Jul 16 '11 at 08:09
  • 2
    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. – Bo Persson Jul 16 '11 at 08:21
  • @Bo Persson: After seeing the comments post the Q.I think the OP mixed it a bit by initializing the pointer to a `NULL` Now i think what s/he really wanted to know was how does compiler handler the reference if it is referencing to a pointer type. – Alok Save Jul 16 '11 at 08:25
  • @ALs- ok. So in this case, whether dereferencing operation will happen or not that depends on compiler implementation of initiazing an reference. Right? – G Mann Jul 16 '11 at 08:29
  • 1
    @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. – Bo Persson Jul 16 '11 at 08:34
  • Thats what i was looking for. Now it make sense for me. Thanks ALs and Bo Persson.. – G Mann Jul 16 '11 at 08:37
  • 4
    "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. – James McNellis Jul 16 '11 at 15:37
  • @James McNellis: Thanks James for reading between the lines! As usual an excellent comment and nicely spotted. I modified the answer to reflect the same. – Alok Save Jul 16 '11 at 15:42
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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 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; – G Mann Jul 16 '11 at 08:04
  • 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. – James McNellis Jul 16 '11 at 08:08
  • Correct me i am wrong. Where it is implemented as pointer then compiler may not be doing the dereferencing operation. – G Mann Jul 16 '11 at 08:27
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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
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

禪 師 無
  • 422
  • 2
  • 7
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();
dimba
  • 26,717
  • 34
  • 141
  • 196