3

Does the following snippet compile/execute the block in the if-statement?

int* pointer = NULL; int deref = *pointer; if(deref == NULL){ // will execute? } 

Since the pointer variable contains NULL does the dereference of this pointer variable also return NULL or will this result in a runtime error?

6
  • 1
    in cannot be dereferenced Commented Sep 21, 2016 at 5:36
  • 1
    @LưuVĩnhPhúc The ISO C standard doesn't impose any requirements like "cannot happen" to the dereferencing of a null pointer. It is undefined behavior, and in fact can be done in some environments and can be provided as a documented language extension (though not a very well considered one, obviously). Commented Sep 21, 2016 at 5:39
  • Historically, it has happened that C programmers exploited dereference-able null pointers to simplify the cases in linked list or tree data structures. you can do things like head_node->next->prev = that without worrying that nead_node->next is the terminating null link. Obviously, that code was found not to be portable to environments that trap null references, oops! It wasn't wrong; it worked fine on the original target platforms, and code was shipped to happy end users. It was nonportable. Commented Sep 21, 2016 at 5:43
  • @Kaz C is not a mentioned tag - just C++. Commented Sep 21, 2016 at 8:06
  • @LưuVĩnhPhúc "cannot" isn't entirely correct. It can be done, but the result is undefined. Commented Sep 21, 2016 at 8:08

3 Answers 3

3

The result is "undefined behaviour", which may or may not trigger a runtime error, and should in any case always be avoided.

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

Comments

2

Once you set:

int* pointer = NULL; 

pointer points to nothing. Now when you write this:

int deref = *pointer; 

deref will try to access what pointer points to, which will lead to an undefined behaviour like segmentation fault.

I hope this explains.

Comments

1

The current answers addressed the UB very well. However, I want to add something. If you run this code:

if(0==NULL){ std::cout << "True"; } 

It will prints True. So if dereferencing a null pointer on your specific environment leads to returning 0 (which is not a steady case. It is UB), the part inside your if statement will execute.

I just wanted to clarify why it is working on some machines. However, that does not change anything about the fact that it is UB.

4 Comments

Just pointing out that your example doesn't dereference NULL, but the following paragraph does talk about that. What was your intent?
@JohannGerell I just wanted to inform the OP that his code might work on some machines and execute the instructions inside the if-statement. This behaviour still undefined but some coders may think that it is defined since it is working and behave like theirs intention. So, I see it worth to tell.
What you're addressing then is the case when the '0' in your example if(0==NULL) happens to be the result of dereferencing a null pointer on some platform some time, right? I was confused by how you formulated this.
Yup exactly that what I meant

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.