0
0
CppComparisonBeginner · 3 min read

Nullptr vs NULL in C++: Key Differences and Usage

In C++, nullptr is a keyword introduced in C++11 to represent a null pointer with type safety, while NULL is a macro typically defined as 0 or ((void*)0) and can cause ambiguity in overload resolution. nullptr is preferred for clear, safe pointer initialization and comparisons.
⚖️

Quick Comparison

This table summarizes the main differences between nullptr and NULL in C++.

AspectnullptrNULL
Introduced inC++11 standardPre-C++11 (macro)
TypePointer literal type std::nullptr_tInteger literal (usually 0)
Type safetyYes, no ambiguity in overloadsNo, can be confused with integer 0
Usage in overload resolutionClear and unambiguousCan cause ambiguity errors
Recommended usagePreferred modern null pointerLegacy, avoid in new code
DefinitionKeyword built into languageMacro usually defined as 0 or ((void*)0)
⚖️

Key Differences

nullptr was introduced in C++11 to provide a dedicated null pointer constant with its own type, std::nullptr_t. This means it can only be assigned to pointer types and cannot be confused with integers, making code safer and clearer.

On the other hand, NULL is a macro that expands to an integer literal 0 or sometimes ((void*)0). Because of this, it can be mistakenly used in contexts expecting integers, leading to ambiguous function overloads or subtle bugs.

Using nullptr improves overload resolution by clearly indicating a null pointer, whereas NULL can cause compiler errors or unexpected behavior when functions are overloaded with pointer and integer parameters.

⚖️

Code Comparison

Here is an example showing how nullptr is used to safely assign and compare null pointers.

cpp
#include <iostream>

void func(int) {
    std::cout << "Function with int called" << std::endl;
}

void func(char*) {
    std::cout << "Function with char* called" << std::endl;
}

int main() {
    char* p = nullptr;  // Safe null pointer
    if (p == nullptr) {
        std::cout << "Pointer is null" << std::endl;
    }
    func(nullptr);  // Calls func(char*) unambiguously
    return 0;
}
Output
Pointer is null Function with char* called
↔️

NULL Equivalent

This example shows how using NULL can cause ambiguity and less safe code.

cpp
#include <iostream>

void func(int) {
    std::cout << "Function with int called" << std::endl;
}

void func(char*) {
    std::cout << "Function with char* called" << std::endl;
}

int main() {
    char* p = NULL;  // NULL is usually 0
    if (p == NULL) {
        std::cout << "Pointer is null" << std::endl;
    }
    // func(NULL);  // Ambiguous call, may cause compile error
    func(0);       // Calls func(int)
    return 0;
}
Output
Pointer is null Function with int called
🎯

When to Use Which

Choose nullptr when writing modern C++ code for clear, type-safe null pointer usage and to avoid ambiguity in function calls. It is the recommended standard since C++11.

Use NULL only when maintaining legacy code that predates C++11 or when working with older compilers that do not support nullptr. Otherwise, avoid NULL to prevent subtle bugs and confusion.

Key Takeaways

nullptr is a type-safe null pointer introduced in C++11 and should be preferred.
NULL is a macro usually defined as 0 and can cause ambiguity in overloads.
Use nullptr for clearer, safer pointer initialization and comparisons.
Avoid NULL in new code to prevent bugs and improve readability.
Legacy code may still use NULL, but modern C++ favors nullptr.