CPlusPlus / Pointers#
What advantages does
nullptrprovide overNULLor using the value0?nullptr is a pointer literal of type std::nullptr_t, which can be implicitly converted to any pointer type. This makes it more type-safe than NULL or 0. Additionally, nullptr cannot be implicitly converted to integral types, helping to prevent certain programming errors.
What happens if you dereference a null pointer?
It results in undefined behavior, often causing a program crash.
How do you dynamically allocate memory for a single integer in C++?
int* ptr = new int;
How do you free dynamically allocated memory in C++?
Use ‘delete’ for single objects and ‘delete[]’ for arrays.
What is the difference between int const ptr and const int* ptr?*
int* const ptr is a constant pointer to an int, while const int* ptr is a pointer to a constant int.