a value indicating that a pointer does not refer to a valid object
In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value indicating that the pointer or reference does not refer to an object. Programs routinely use … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Null_pointer
Null pointer - Wikipedia
2 weeks ago - The C standard does not say that the null pointer is the same as the pointer to memory address 0, though that may be the case in practice. Dereferencing a null pointer is undefined behavior in C, and a conforming implementation is allowed to assume that any pointer that is dereferenced is not null.
Top answer
1 of 8
118

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.

2 of 8
56

Dereferencing just means accessing the memory value at a given address. So when you have a pointer to something, to dereference the pointer means to read or write the data that the pointer points to.

In C, the unary * operator is the dereferencing operator. If x is a pointer, then *x is what x points to. The unary & operator is the address-of operator. If x is anything, then &x is the address at which x is stored in memory. The * and & operators are inverses of each other: if x is any data, and y is any pointer, then these equations are always true:

*(&x) == x
&(*y) == y

A null pointer is a pointer that does not point to any valid data (but it is not the only such pointer). The C standard says that it is undefined behavior to dereference a null pointer. This means that absolutely anything could happen: the program could crash, it could continue working silently, or it could erase your hard drive (although that's rather unlikely).

In most implementations, you will get a "segmentation fault" or "access violation" if you try to do so, which will almost always result in your program being terminated by the operating system. Here's one way a null pointer could be dereferenced:

int *x = NULL;  // x is a null pointer
int y = *x;     // CRASH: dereference x, trying to read it
*x = 0;         // CRASH: dereference x, trying to write it

And yes, dereferencing a null pointer is pretty much exactly like a NullReferenceException in C# (or a NullPointerException in Java), except that the langauge standard is a little more helpful here. In C#, dereferencing a null reference has well-defined behavior: it always throws a NullReferenceException. There's no way that your program could continue working silently or erase your hard drive like in C (unless there's a bug in the language runtime, but again that's incredibly unlikely as well).

Discussions

What happens when dereferencing a nullptr?
Dereferencing a null pointer is undefined behavior. In practice, trying to dereference null usually results in a seg-fault, but sometimes the compiler can optimize out the operation entirely. In your example, *p == true; doesn't actually change any of the program state, so the compiler is being smart and removing the extra computation. In the cout line, your program is actually using the result of the computation so it can't be removed. Note: In some cases an aggressive optimizer may recognize that dereferencing a null pointer would be undefined behavior and assume that the pointer therefore cannot be null. This can lead to some unintuitive and hard to find bugs. More on reddit.com
🌐 r/cpp_questions
20
14
August 18, 2022
Dereferencing null pointers - what does the standard say?
https://eel.is/c++draft/class.mfct.non-static If a non-static member function of a class X is called for an object that is not of type X, or of a type derived from X, the behavior is undefined. More on reddit.com
🌐 r/cpp_questions
40
14
April 20, 2021
What happens in OS when we dereference a NULL pointer in C? - Stack Overflow
But sometimes it's possible to change that, e.g. on Linux by writing 0 to the pseudofile /proc/sys/vm/mmap_min_addr, after which it's possible to use mmap(2) to map the virtual address 0. In that case, dereferencing a null pointer would not cause a page fault. More on stackoverflow.com
🌐 stackoverflow.com
C++ standard: dereferencing NULL pointer to get a reference? - Stack Overflow
I'm wondering about what the C++ standard says about code like this: int* ptr = NULL; int& ref = *ptr; int* ptr2 = &ref; In practice the result is that ptr2 is NULL but I'm wondering, is t... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 433166 › dereferencing-null-pointer-in-c-visual-studio-2019
Dereferencing NULL pointer in C - Visual Studio 2019 - Microsoft Q&A
June 11, 2021 - int n; scanf_s("%d", &n); int** dArray = calloc(n, sizeof(int*)); if(dArray) { for (int r = 0; r < n; r++) { *(dArray + r) = calloc((r + 1), sizeof(int)); } } ... I suggest you read the guidance about validating a pointer before you use it in ...
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › c › EXP34-C.+Do+not+dereference+null+pointers
EXP34-C. Do not dereference null pointers | CERT Secure Coding
In this noncompliant code example, input_str is copied into dynamically allocated memory referenced by c_str . If malloc() fails, it returns a null pointer that is assigned to c_str . When c_str is dereferenced in memcpy() , the program exhibits undefined behavior .
🌐
Reddit
reddit.com › r/cpp_questions › what happens when dereferencing a nullptr?
r/cpp_questions on Reddit: What happens when dereferencing a nullptr?
August 18, 2022 -

I saw this code in A Tour of C++, but with a bit modify for illustration:

#include <iostream>

int main() {
  char s = 'a';
  char *p = &s;
  while (*p) {
    std::cout << *p;
    p++;
  }
  p = nullptr;
  //std::cout << (*p == true);
  *p == true;
}

I do not know how does while (*p) { end while I do not know what happens when p is nullptr. And std::cout << (*p == true) will induce segment fault but *p == true does not.

🌐
Rip Tutorial
riptutorial.com › dereferencing a null pointer
C Language Tutorial => Dereferencing a null pointer
int * pointer = NULL; int value = *pointer; /* Dereferencing happens here */ A NULL pointer is guaranteed by the C standard to compare unequal to any pointer to a valid object, and dereferencing it invokes undefined behavior.
Find elsewhere
🌐
Snyk Learn
learn.snyk.io › home › security education › what is a null dereference? | tutorial & examples
What is a null dereference? | Tutorial & examples | Snyk Learn
August 15, 2024 - Pointers are variables that store the memory address of an object, and a null pointer dereference occurs when you try to access an object at a memory address that is null. In languages that use pointers, such as C and C++, null pointer dereferences ...
🌐
Mayhem Security
mayhem.security › blog › what-is-null-pointer-dereference
What Is Null Pointer Dereference? | Mayhem
June 1, 2022 - Null pointer dereferences are particularly common in C and C++ programs, since these languages do not automatically check for NULL pointers.
🌐
Sivanesh Waran
sivaneshwaran.com › home › klocwork › null pointer dereference in c
Null Pointer Dereference in C Null Pointer Dereference in C
May 5, 2023 - May 3, 2023 / By Sivanesh. Null pointer dereference is a common programming error that occurs when a program attempts to dereference a pointer that points to null or undefined memory.
🌐
Reddit
reddit.com › r/cpp_questions › dereferencing null pointers - what does the standard say?
r/cpp_questions on Reddit: Dereferencing null pointers - what does the standard say?
April 20, 2021 -
01: #include <iostream>
02: 
03: class greeter
04: {
05: public:
06:     void hello()
07:     {
08:     std::cout << "Hello world";
09:     }
10: };
11: 
12: int main()
13: {
14:     ((greeter*)nullptr)->hello();
15: }

runs with no warnings on -Weveryting -Wall on gcc, no warnings on MSVC /W4 either.

https://godbolt.org/z/779Y4Ejzz

I'm sitting with the standard open but I must admit this is taking me forever to find. Do any of you know where to look?

EDIT: So far in my own research, I've got this from 21 years ago:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232

At one point we agreed that dereferencing a null pointer was not undefined; only using the resulting value had undefined behavior.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
January 10, 2025 - To check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer-related code, e.g., dereference a pointer variable only if it’s not NULL.
🌐
MITRE
cwe.mitre.org › data › definitions › 476.html
CWE - CWE-476: NULL Pointer Dereference (4.20)
Current News Blog Podcast News Archive CWE Board Working Groups & Special Interest Groups Email Lists
Top answer
1 of 5
76

Short answer: it depends on a lot of factors, including the compiler, processor architecture, specific processor model, and the OS, among others.

Long answer (x86 and x86-64): Let's go down to the lowest level: the CPU. On x86 and x86-64, that code will typically compile into an instruction or instruction sequence like this:

movl $10, 0x00000000

Which says to "store the constant integer 10 at virtual memory address 0". The Intel® 64 and IA-32 Architectures Software Developer Manuals describe in detail what happens when this instruction gets executed, so I'm going to summarize it for you.

The CPU can operate in several different modes, several of which are for backwards compatibility with much older CPUs. Modern operating systems run user-level code in a mode called protected mode, which uses paging to convert virtual addresses into physical addresses.

For each process, the OS keeps a page table which dictates how the addresses are mapped. The page table is stored in memory in a specific format (and protected so that they can not be modified by the user code) that the CPU understands. For every memory access that happens, the CPU translates it according to the page table. If the translation succeeds, it performs the corresponding read/write to the physical memory location.

The interesting things happen when the address translation fails. Not all addresses are valid, and if any memory access generates an invalid address, the processor raises a page fault exception. This triggers a transition from user mode (aka current privilege level (CPL) 3 on x86/x86-64) into kernel mode (aka CPL 0) to a specific location in the kernel's code, as defined by the interrupt descriptor table (IDT).

The kernel regains control and, based on the information from the exception and the process's page table, figures out what happened. In this case, it realizes that the user-level process accessed an invalid memory location, and then it reacts accordingly. On Windows, it will invoke structured exception handling to allow the user code to handle the exception. On POSIX systems, the OS will deliver a SIGSEGV signal to the process.

In other cases, the OS will handle the page fault internally and restart the process from its current location as if nothing happened. For example, guard pages are placed at the bottom of the stack to allow the stack to grow on demand up to a limit, instead of preallocating a large amount of memory for the stack. Similar mechanisms are used for achieving copy-on-write memory.

In modern OSes, the page tables are usually set up to make the address 0 an invalid virtual address. But sometimes it's possible to change that, e.g. on Linux by writing 0 to the pseudofile /proc/sys/vm/mmap_min_addr, after which it's possible to use mmap(2) to map the virtual address 0. In that case, dereferencing a null pointer would not cause a page fault.

The above discussion is all about what happens when the original code is running in user space. But this could also happen inside the kernel. The kernel can (and is certainly much more likely than user code to) map the virtual address 0, so such a memory access would be normal. But if it's not mapped, then what happens then is largely similar: the CPU raises a page fault error which traps into a predefined point at the kernel, the kernel examines what happened, and reacts accordingly. If the kernel can't recover from the exception, it will typically panic in some fashion (kernel panic, kernel oops, or a BSOD on Windows, e.g.) by printing out some debug information to the console or serial port and then halting.

See also Much ado about NULL: Exploiting a kernel NULL dereference for an example of how an attacker could exploit a null pointer dereference bug from inside the kernel in order to gain root privileges on a Linux machine.

2 of 5
6

As a side note, just to compel the differences in architectures, a certain OS developed and maintained by a company known for their three-letter acronym name and often referred to as a large primary color has a most-fasicnating NULL determination.

They utilize a 128-bit linear address space for ALL data (memory AND disk) in one giant "thing". In accordance with their OS, a "valid" pointer must be placed on a 128-bit boundary within that address space. This, btw, causes fascinating side effects for structs, packed or not, that house pointers. Anyway, tucked away in a per-process dedicated page is a bitmap that assigns one bit for every valid location in a process address space where a valid pointer can lay. ALL opcodes on their hardware and OS that can generate and return a valid memory address and assign it to a pointer will set the bit that represents the memory address where that pointer (the target pointer) is located.

So why should anyone care? For this simple reason:

int a = 0;
int *p = &a;
int *q = p-1;

if (p)
{
// p is valid, p's bit is lit, this code will run.
}

if (q)
{
   // the address stored in q is not valid. q's bit is not lit. this will NOT run.
}

What is truly interesting is this.

if (p == NULL)
{
   // p is valid. this will NOT run.
}

if (q == NULL)
{
   // q is not valid, and therefore treated as NULL, this WILL run.
}

if (!p)
{
   // same as before. p is valid, therefore this won't run
}

if (!q)
{
   // same as before, q is NOT valid, therefore this WILL run.
}

Its something you have to see to believe. I can't even imagine the housekeeping done to maintain that bit map, especially when copying pointer values or freeing dynamic memory.

🌐
Unstop
unstop.com › home › blog › null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - This means that uninitializedPtr ... to dereference uninitializedPtr by assigning the value 42 to the memory location it points to using the indirection/ dereferencing operator (*)....
🌐
Secure Coding Blog
blog.bytehackr.in › understanding-and-preventing-null-pointer-dereference
Top 5 Way to Prevent NULL Pointer Dereference
May 8, 2025 - Memory allocation failures: Dynamic memory allocation functions like malloc() in C/C++ return NULL when they fail to allocate the requested memory. If the program does not handle this failure properly and attempts to use the returned NULL pointer, a null pointer dereference can occur.
🌐
Quora
quora.com › What-is-dereferencing-a-null-pointer-in-the-C-language
What is dereferencing a null pointer in the C language? - Quora
Answer: A NULL pointer is intended to be used to indicate that nothing is being pointed to. For example, in a singly-linked list, the pointer from one element to the next element will typically be a NULL pointer in the last element in the list. Similarly, a leaf node in a binary tree will have tw...
🌐
Quora
quora.com › How-do-I-avoid-dereferencing-null-pointers-in-C
How to avoid dereferencing null pointers in C - Quora
Answer (1 of 4): This is like asking “How do I avoid getting run over when I cross a street?” The answer? You check for cars before crossing. To avoid dereferencing a NULL pointer, check to make sure it’s not NULL before dereferencing it. That’s it. No fancy tricks.
🌐
Quora
quora.com › What-does-it-mean-when-you-dereference-a-pointer-and-its-null
What does it mean when you dereference a pointer and it's null? - Quora
Answer (1 of 3): It means that in C the program will crash with a segmentation fault; it is not allowed to access address [code ]0x0000000000[/code] in memory for user programs. In a language that uses pointers but have exception mechanisms ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 71010
Dereferencing Null Pointer - C++ Forum
If you were trying to dereference a null pointer, that would say location 0x00000000. ... The error messages don't show anything about a null pointer. You should look at the beginning of your function b2World::Step(). It seems, you used a deallocated pointer or called a function, which returned a pointer to local data within the called function, and which now are destroyed and deallocated.