Nullptr vs NULL in C++: Key Differences and Usage
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++.
| Aspect | nullptr | NULL |
|---|---|---|
| Introduced in | C++11 standard | Pre-C++11 (macro) |
| Type | Pointer literal type std::nullptr_t | Integer literal (usually 0) |
| Type safety | Yes, no ambiguity in overloads | No, can be confused with integer 0 |
| Usage in overload resolution | Clear and unambiguous | Can cause ambiguity errors |
| Recommended usage | Preferred modern null pointer | Legacy, avoid in new code |
| Definition | Keyword built into language | Macro 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.
#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; }
NULL Equivalent
This example shows how using NULL can cause ambiguity and less safe code.
#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; }
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.nullptr for clearer, safer pointer initialization and comparisons.NULL in new code to prevent bugs and improve readability.NULL, but modern C++ favors nullptr.