๐ŸŒ
Learn C++
learncpp.com โ€บ cpp-tutorial โ€บ null-pointers
12.8 โ€” Null pointers โ€“ Learn C++
August 12, 2015 - Use nullptr when you need a null pointer literal for initialization, assignment, or passing a null pointer to a function. Dereferencing a null pointer results in undefined behavior
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
June 13, 2026 - Because a null pointer does not point to a meaningful object, an attempt to access the data stored at that (invalid) memory location may cause a run-time error or immediate program crash. This is the null pointer error, or null pointer exception.
Discussions

c++ - Dealing with accessing NULL pointer - Stack Overflow
But it didn't help. I have to initialize it as NULL in constructor. It's part of a larger project, but I think, the code above is what makes me trouble. I'm still pretty new in C++/OOP and sometimes I still get confused by pointers. More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Accessing NULL pointers - Stack Overflow
This is part of my code... and i have several major problems that im stuck on... ok most of the problem that i can see so far has been fixed... but im still lost on where to free the memory. this i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Trying to understand NULL pointers
NULL is equivalent to 0 and equivalent to nullptr (in C++). It's an address that is "universally invalid." You can store 0x2093704802934 in a pointer and it will be invalid too (I'm sure) but it's not a convention. NULL is this convention that says "the pointer is not valid" and/or "the pointer hasn't been initialized yet" or "the memory pointed to has been released and I have invalidated the pointer by setting it to NULL." You store this value in a "pointer", like "int* s = 0" to show that it is not pointing to anything. And in your code you will check whether the pointer (s in my example) is equal to NULL (invalid) or not (valid). First you create a pointer, then you initialize it, then you use it, then you release it. More on reddit.com
๐ŸŒ r/learnprogramming
18
1
December 30, 2021
c - How to understand accessing NULL pointer when using in sizeof? - Stack Overflow
(Note that there is a special case ... invalid pointer to a VLA inside sizeof would not be safe.) As a side note, the correct format specifier for printf when printing the result of sizeof is %zu (the result of sizeof is type size_t). ... Sign up to request clarification or add additional context in comments. ... Because sizeof(exp) is a compile time operator, and it does not evaluate expression exp at run-time. As a result, there is no dereference of NULL pointer at ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Quora
quora.com โ€บ What-happens-when-we-try-to-access-a-null-pointer-in-C
What happens when we try to access a null pointer in C? - Quora
Answer (1 of 4): The standard says that accessing a NULL ptr is โ€œundefined behaviorโ€. Undefined behavior can be anything, including: * Nothing at all - continue running the program as if nothing happened * Crashing the application * Corrupting application data From Wikipedia we have this: ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
To check for a null pointer before accessing any pointer variable.
Published: January 10, 2025
๐ŸŒ
Medium
medium.com โ€บ @kiranbjm โ€บ what-is-a-null-pointer-exception-de351fb907e7
What is a Null Pointer Exception? | by Kiran Benny Joseph | Medium
October 26, 2024 - What is a Null Pointer Exception? A NullPointerException happens when your code tries to access properties, methods, or functions on a variable that hasnโ€™t been assigned a reference in memory โ€ฆ
Find elsewhere
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ cplusplus โ€บ cpp_null_pointers.htm
C++ Null Pointers
The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program โˆ’ ยท #include <iostream> using namespace std; int main () { int *ptr = NULL; cout << "The value of ptr is " << ptr ; return 0; } When the above code is compiled and executed, it produces the following result โˆ’ ... On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ null-pointer-exception-in-java
Null Pointer Exception in Java - GeeksforGeeks
1 week ago - A NullPointerException (NPE) is a runtime exception in Java that occurs when a program tries to access or use an object reference that has the value null.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ trying to understand null pointers
r/learnprogramming on Reddit: Trying to understand NULL pointers
December 30, 2021 -

Hello all again,

I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ null-pointer-in-cpp
NULL Pointer in C++ - GeeksforGeeks
It is commonly used during variable declaration to show that a pointer is not yet associated with any memory, and it is also returned by many built-in functions to indicate a failure condition.
Published: June 8, 2026
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_null.php
C NULL
NULL is a special value that represents a "null pointer" - a pointer that does not point to anything.
๐ŸŒ
Rollbar
rollbar.com โ€บ home โ€บ how to catch and fix nullpointerexception in java
How to Catch and Fix NullPointerException in Java
June 30, 2026 - It occurs when your code tries to use a variable that doesn't point to any object and instead refers to nothing (null). Think of it like trying to open a door that doesn't exist. You reach for the handle, but there's nothing thereโ€”just empty space.
๐ŸŒ
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 - We also use the pointer ptr to access the elements of the array at index position i, and the %d format specifier in the formatted string represents an integer value. The printf() statement with the newline escape sequence shifts the cursor to the next line. After using the allocated memory, we free it using free() function to release it back to the system. As mentioned before, a null pointer in C is a special pointer that does not point to any valid memory address.
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Null-Pointers.html
Null Pointers (GNU C Language Manual)
A pointer value can be null, which means it does not point to any object. The cleanest way to get a null pointer is by writing NULL, a standard macro defined in stddef.h. You can also do it by casting 0 to the desired pointer type, as in (char *) 0.
๐ŸŒ
ScienceDirect
sciencedirect.com โ€บ topics โ€บ computer-science โ€บ null-pointer
Null Pointer - an overview | ScienceDirect Topics
As in Figure 7.11, a diagonal slash through a box indicates a null pointer. ... To access the object referred to by a pointer, most languages use an explicit dereferencing operator.
Top answer
1 of 6
30

In C and C++, pointers are inherently unsafe, that is, when you dereference a pointer, it is your own responsibility to make sure it points somewhere valid; this is part of what "manual memory management" is about (as opposed to the automatic memory management schemes implemented in languages like Java, PHP, or the .NET runtime, which won't allow you to create invalid references without considerable effort).

A common solution that catches many errors is to set all pointers that don't point to anything as NULL (or, in correct C++, 0), and checking for that before accessing the pointer. Specifically, it is common practice to initialize all pointers to NULL (unless you already have something to point them at when you declare them), and set them to NULL when you delete or free() them (unless they go out of scope immediately after that). Example (in C, but also valid C++):

void fill_foo(int* foo) {
    *foo = 23; // this will crash and burn if foo is NULL
}

A better version:

void fill_foo(int* foo) {
    if (!foo) { // this is the NULL check
        printf("This is wrong\n");
        return;
    }
    *foo = 23;
}

Without the null check, passing a NULL pointer into this function will cause a segfault, and there is nothing you can do - the OS will simply kill your process and maybe core-dump or pop up a crash report dialog. With the null check in place, you can perform proper error handling and recover gracefully - correct the problem yourself, abort the current operation, write a log entry, notify the user, whatever is appropriate.

2 of 6
8

The other answers pretty much covered your exact question. A null check is made to be sure that the pointer you received actually points to a valid instance of a type (objects, primitives, etc).

I'm going to add my own piece of advice here, though. Avoid null checks. :) Null checks (and other forms of Defensive Programming) clutter code up, and actually make it more error prone than other error-handling techniques.

My favorite technique when it comes to object pointers is to use the Null Object pattern. That means returning a (pointer - or even better, reference to an) empty array or list instead of null, or returning an empty string ("") instead of null, or even the string "0" (or something equivalent to "nothing" in the context) where you expect it to be parsed to an integer.

As a bonus, here's a little something you might not have known about the null pointer, which was (first formally) implemented by C.A.R. Hoare for the Algol W language in 1965.

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ c++ trying to access a null pointer to an object "locks up" the rest of the program
r/learnprogramming on Reddit: C++ trying to access a null pointer to an object "locks up" the rest of the program
January 6, 2021 -

I did a bit of googling and looking around on this reddit but I couldn't seem to find an answer to this problem. If I have a pointer that points to a null pointer and I try to access some part of that pointer with std::cout, then it won't do any other std::cout for the rest of the code. I have written a dummy program that demonstrates what is happening.

#include <iostream>

class Car {
    public:
        int year;

        Car() {
            year = 2021;
        }
};

int main() {

    Car lemon;
    Car* pointer = nullptr;

    lemon.year = 2000;

    std::cout << lemon.year << "\n";
    std::cout << pointer -> year << "\n";
    std::cout << "I print when the above line is commented out\n";

    return 0;
}

In this code, the output will show lemon.year (2000) only once! It will execute the first std::cout but not the second or third. When I comment out the std::cout << pointer -> year << "\n"; THEN the third std::cout will print.

I'm not sure why this is happening. I've never really liked pointers but the problem I am working on sort of demands that I use pointers within my class (for anyone curious I am building a trie https://en.wikipedia.org/wiki/Trie)