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
c - What exactly is meant by "dereferencing a NULL pointer"? - Stack Overflow
You might want to put down some example code. It seems that people (including me) don't get what you are trying to ask. ... No need for code (there isn't any) - This is a conceptual problem I am having, trying to get my head around the terminology of "dereferencing" and why I should be caring about it. ... Save this answer. ... Show activity on this post. A NULL pointer ... More on stackoverflow.com
🌐 stackoverflow.com
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
Explain the concept of "null pointer dereference" and how to prevent it in C++.
Use a memory leak detection tool to find and fix null pointer dereferences in your code. Here is an example of how to prevent null pointer dereference in C++: More on mindstick.com
🌐 mindstick.com
0
August 16, 2023
🌐
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 - At last, in the example of Marc and the extension review code, a pointer was not even needed. By using a non-pointer integer as stars, the code would have never been exposed to null pointer dereferences.
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 - There are occasions when dereferencing a pointer to address zero is intentional and well-defined; for example, BIOS code written in C for 16-bit real-mode x86 devices may write the interrupt descriptor table (IDT) at physical address 0 of the machine by dereferencing a pointer with the same value as a null pointer for writing.
🌐
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.

🌐
OWASP Foundation
owasp.org › www-community › vulnerabilities › Null_Dereference
Null Dereference | OWASP Foundation
This is an example of a Project or Chapter Page. CWE-476: NULL Pointer Dereference: 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.
Find elsewhere
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).

🌐
Medium
medium.com › @chanibonner › a-beginners-guide-to-null-pointer-dereference-attacks-d3618cc8a493
A Beginner’s Guide to Null Pointer Dereference Attacks | by Chani Bonner | Medium
February 25, 2024 - That’s what happens when a pointer has a null value. Let’s see what happens next. Now that you have an idea of what a NPD is, imagine for a minute someone tries to dereference a null pointer in the same way they would a pointer that contains a valid memory address.
🌐
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 .
🌐
Mayhem Security
mayhem.security › blog › what-is-null-pointer-dereference
What Is Null Pointer Dereference? | Mayhem
June 1, 2022 - CWE-476 Null Pointer Dereference is a programming error that can occur when a program attempts to deference a null pointer. This can happen when the programmer mistakenly assumes that a pointer pointing to NULL is actually pointing to a valid object.
🌐
Secure Coding Blog
blog.bytehackr.in › understanding-and-preventing-null-pointer-dereference
Top 5 Way to Prevent NULL Pointer Dereference
May 8, 2025 - To avoid a null pointer dereference, it is essential to ensure that pointers are properly initialized and assigned valid memory addresses before dereferencing them. For instance, in the example above, assigning ptr the address of a valid integer variable would prevent the null pointer dereference:
🌐
AWS
docs.aws.amazon.com › codeguru › detector-library › java › null-pointer-dereference
Null pointer dereference | Amazon Q, Detector Library
1private Double nullCheckPointerNoncompliant(@Nullable Double digit) { 2 // Noncompliant: avoids null checks before dereferencing the pointer.
🌐
Fortify
vulncat.fortify.com › en › detail
Software Security | Null Dereference - Fortify Taxonomy
If ptr can be NULL when it is checked in the if statement then it can also be NULL when it dereferenced and may cause a segmentation fault. ptr->field = val; ... if (ptr != NULL) { ... } Example 2: In the following code, the programmer confirms that the variable ptr is NULL and subsequently ...
🌐
Medium
thehacktivists.medium.com › null-pointer-dereference-cwe-476-the-hacktivists-d3ceb2a7ade1
NULL Pointer Dereference [CWE-476] — The Hacktivists | by The Hacktivists | Medium
March 13, 2021 - If a high-privileged application, ... the crash of such application may render the system inaccessible: 4.9 (AV:L/AC:L/Au:N/C:N/I:N/A:C) — Medium severity....
🌐
White Knight Labs
whiteknightlabs.com › 2025 › 06 › 24 › understanding-null-pointer-dereference-in-windows-kernel-drivers
Understanding Null Pointer Dereference in Windows Kernel Drivers | White Knight Labs
June 24, 2025 - The Windows Debugger shows a crash in the DispatchIoctl function of my custom driver, specifically at the instruction mov dword ptr [rdi], 0xDEADBEEF, where RDI is NULL. This confirms a classic NULL pointer dereference, as the kernel attempts ...
🌐
MCSI Library
library.mosse-institute.com › articles › 2023 › 07 › null-pointer-dereferencing.html
Null Pointer Dereferencing — MCSI Library
Null pointer dereferencing happens when a program attempts to read or write to a memory location pointed to by a null pointer, which doesn’t point to any valid memory address. Example: Consider the following C/C++ code snippet:
🌐
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.

🌐
Qnx
qnx.com › developers › docs › qnxcar2 › topic › com.qnx.doc.ide.userguide › topic › memory_Null_pointderef_.html
NULL pointer dereference
To detect the freeing of a zero (0) pointer, select Enable check on realloc()/free() argument. ... In the IDE, you can expect the message for this type of memory error to include the following types of information and detail: ... TrapFunction: shows the memory or string function where the error occurred. ... For a list of error messages returned by the Memory Analysis tool, see Summary of error messages for Memory Analysis. ... You can perform an explicit check for NULL for all pointers returned by functions that can return NULL, and when parameters are passed to the function.
🌐
MindStick
mindstick.com › forum › 159532 › explain-the-concept-of-null-pointer-dereference-and-how-to-prevent-it-in-c-plus-plus
Explain the concept of "null pointer dereference" and how to prevent it in C++. – MindStick
August 16, 2023 - Use a memory leak detection tool to find and fix null pointer dereferences in your code. Here is an example of how to prevent null pointer dereference in C++: