difference between NULL and nullptr in c++
NULL and nullptr are both used to represent null or empty values in C++, but they have some key differences.
- Type: NULL is a macro that is defined as 0 or ((void*)0), depending on the compiler. This means that NULL has the type of an integer, typically int or long. nullptr is a keyword that has the type std::nullptr_t. This type is specifically designed to represent null pointers, and it is not implicitly convertible to other types.
- Use cases: nullptr should be used in all cases where you need to represent a null pointer. This includes initializing pointers, passing pointers to functions, and comparing pointers for equality. NULL can still be used, but it is recommended to use nullptr instead, especially in new code.
- Advantages of nullptr: nullptr has several advantages over NULL:
- It is more type-safe. Because nullptr has the type std::nullptr_t, it cannot be implicitly converted to other types, which can help to prevent errors.
- It is more portable. The definition of NULL is not guaranteed to be consistent across all compilers, but the definition of nullptr is. This makes it safer to use nullptr in portable code.
- It is more expressive. nullptr clearly indicates that the value of a pointer is null, while NULL can be interpreted as either a null pointer or an integer value of 0.
- Function overloading: NULL can cause ambiguity in function overloading. For example, consider the following two functions:
C++
void f(int); void f(void*);If we call the function f()
with the argument NULL
, the compiler will not be able to determine which function to call. This is because NULL can be implicitly converted to both int and void*.
nullptr does not cause this ambiguity because it cannot be implicitly converted to integral types. Therefore, if we call the function f()
with the argument nullptr
, the compiler will know to call the function that takes a void* argument.
Comparison with other types: nullptr can only be compared to other pointers, while NULL can be compared to any integer type. This can lead to unexpected behavior if we are not careful. For example, the following code will compile, but it is likely to produce unexpected results:
int x = 0; bool isEqual = (NULL == x);This is because NULL can be implicitly converted to int, so the compiler will treat the comparison as an equality comparison between two integers.
nullptr does not have this problem because it cannot be implicitly converted to integral types. Therefore, the following code will not compile:
int x = 0; bool isEqual = (nullptr == x);This will help to prevent errors and make your code more robust.
Conclusion:
nullptr is a more modern and type-safe way to represent null pointers in C++. It is recommended to use nullptr instead of NULL in all new C++ code.
NULL | nullptr |
It is not defined by c++. It is internally mapped to into(zero). Example int f1 (char*); int f1 (int); char*ptr = NULL; // pointer if we have a NULL pointer then function call int f1 (cptr) would always map to f1(int) and not to int f1 (char*). | It has been defined by c++. nullptr is a legal empty/ null pointer. Example int f1 (char*); int f1 (int); char*ptr = nullptr; // pointer if we have a nullptr pointer then function call int f1 (cptr) will map to int f1 (char*) only. |