Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is meant by "de-referencing a NULL pointer"?

Tags:

c

pointers

I am a complete novice to C, and during my university work I've come across comments in code that often refer to de-referencing a NULL pointer. I do have a background in C#, I've been getting by that this might be similar to a "NullReferenceException" that you get in .Net, but now I am having serious doubts.

Can someone please explain to me in layman's terms exactly what this is and why it is bad?

like image 207
Ash Avatar asked Oct 24 '10 05:10

Ash


People also ask

What does dereferencing a null pointer mean?

A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit. Extended Description. NULL pointer dereference issues can occur through a number of flaws, including race conditions, and simple programming omissions.

What does de reference a null object mean?

Description. In Salesforce CPQ, the error 'Attempt to de-reference null object' is caused by a line of code that is searching for a value that does not exist because a particular field value is null. This error can also occur when a required Look-up field is missing a record value.

What happens if I dereference a null pointer?

Dereferencing a null pointer always results in undefined behavior and can cause crashes. If the compiler finds a pointer dereference, it treats that pointer as nonnull. As a result, the optimizer may remove null equality checks for dereferenced pointers.

What does dereferencing a pointer mean?

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.


1 Answers

A NULL pointer points to memory that doesn't exist. This may be address 0x00000000 or any other implementation-defined value (as long as it can never be a real address). Dereferencing it means trying to access whatever is pointed to by the pointer. The * operator is the dereferencing operator:

int a, b, c; // some integers int *pi;     // a pointer to an integer  a = 5; pi = &a; // pi points to a b = *pi; // b is now 5 pi = NULL; c = *pi; // this is a NULL pointer dereference 

This is exactly the same thing as a NullReferenceException in C#, except that pointers in C can point to any data object, even elements inside an array.

like image 66
Greg Hewgill Avatar answered Sep 19 '22 14:09

Greg Hewgill