C++ NULL vs. nullptr: A Guide to Modern Null Pointers

2025-09-25

In C and early C++, NULL is often a macro that expands to the integer literal 0. This can cause several issues

Overload Resolution Ambiguity
Functions can be overloaded for different types. When you pass NULL to an overloaded function, the compiler might not know whether to call the version that takes an integer or the one that takes a pointer. This can lead to a compilation error or, worse, an unexpected function call.

For example, consider this code

void print(int x) {
    std::cout << "Integer version: " << x << std::endl;
}

void print(char* s) {
    std::cout << "Pointer version: " << (s ? s : "nullptr") << std::endl;
}

// This line can be ambiguous depending on the compiler's behavior.
print(NULL);

The compiler might see NULL as 0 and call the int version, even if you intended to pass a null pointer.

Type Safety Issues
Since NULL is an integer, you can accidentally assign it to non-pointer types without a compiler warning, which can lead to tricky bugs. This lack of type safety can make your code harder to debug.

Modern C++ provides a much safer and clearer alternative
nullptr. Introduced in C++11, nullptr is a keyword that represents a null pointer constant. It has its own dedicated type, std::nullptr_t, which makes it distinct from any integer type.

Type Safety
nullptr can only be assigned to a pointer type. If you try to assign it to an integer or any other non-pointer type, the compiler will throw an error, preventing accidental misuse.

No Ambiguity
When used with overloaded functions, nullptr will always resolve to the pointer version of the function, eliminating the ambiguity we saw with NULL.

Here’s the previous example, but with nullptr

void print(int x) {
    std::cout << "Integer version: " << x << std::endl;
}

void print(char* s) {
    std::cout << "Pointer version: " << (s ? s : "nullptr") << std::endl;
}

// The compiler knows exactly what to do here. It will call the pointer version.
print(nullptr);

This code is clear, safe, and works as expected.

If you're converting old code from NULL to modern C++, here are some simple search-and-replace examples

Old (using NULL)New (using nullptr)
MyClass* obj = NULL;MyClass* obj = nullptr;
if (ptr != NULL)if (ptr != nullptr)
void myFunction(void* p = NULL)void myFunction(void* p = nullptr)

cpp



Demystifying std::in_range: Common Pitfalls and Alternatives

std::in_range was introduced in C++20 to simplify checking if a value fits within the range of another type. It can prevent undefined behavior that might occur from implicit type conversions


C++ multimap: A Guide to equal_range

The std::multimap::count method is straightforward it returns the number of elements with a specific key. This is useful


Troubleshooting std::map in C++: Common Problems and Solutions

The std::map is a very useful container in C++ that stores elements in sorted order based on their keys. It's great for when you need to quickly look up values by a key and keep your data organized



Mastering std::formatter for std::chrono::local_info in C++20

Here's a friendly explanation of the class, common troubles, and an alternative, along with some code examples.In C++20


The C++ Vector Capacity Trap: reserve vs. resize Explained

Here is a friendly, detailed breakdown of std::vector::reserve, including common issues and alternative methods.The main purpose of std::vector::reserve is to manage the vector's capacity


Demystifying C++'s transform_reduce Algorithm

One of the most common issues with std::transform_reduce is an incorrect understanding of its parameters, which can lead to compilation errors or unexpected runtime behavior


Mastering tellp() in C++: Pitfalls and Alternatives for Stream Positioning

The function std::basic_ostream::tellp() is part of the C++ Input/Output library. Its main job is to return the current position of the put pointer (the output position indicator) in an output stream


Troubleshooting std::chrono::system_clock::from_time_t in C++

The biggest issue with std::chrono::system_clock::from_time_t is its inaccuracy and lack of time zone awareness.Loss of Precision time_t is a C-style time representation


C++ Coroutines: Managing std::coroutine_handle

The std::coroutine_handle is essentially a non-owning handle or a pointer-like object that points to the coroutine frame of a suspended coroutine


Common Issues and Alternatives for C++ Stack Tracing

std::basic_stacktrace::empty is a member function of the std::basic_stacktrace class in C++. It returns a bool value, indicating whether the stack trace is empty