A point that most of the answers here are not addressing, at least not explicitly, is that a null pointer is a value that exists during execution, and a null pointer constant is a syntactic construct that exists in C source code.
A null pointer constant, as Karlson's answer correctly states, is either an integer constant expression with the value 0 (a simple 0
is the most common example), or such an expression cast to void*
(such as (void*)0
).
NULL
is a macro, defined in <stddef.h>
and several other standard headers, that expands to an implementation-defined null pointer constant. The expansion is typically either 0
or ((void*)0)
(the outer parentheses are needed to satisfy other language rules).
So a literal 0
, when used in a context that requires an expression of pointer type, always evaluates to a null pointer, i.e., a unique pointer value that points to no object. That does not imply anything about the representation of a null pointer. Null pointers are very commonly represented as all-bits-zero, but they can be represented as anything. But even if a null pointer is represented as 0xDEADBEEF
, 0
or (void*)0
is still a null pointer constant.
This answer to the question on stackoverflow covers this well.
This implies, among other things, that memset()
or calloc()
, which can set a region of memory to all-bits-zero, will not necessarily set any pointers in that region to null pointers. They're likely to do so on most implementations, perhaps even all existing ones, but the language doesn't guarantee it.
This question is really a duplicate of this one, but Stack Exchange doesn't allow marking duplicates across sites.
In your example myApple
has the special value null
(typically all zero bits), and so is referencing nothing. The object that it originally referred to is now lost on the heap. There is no way to retrieve its location. This is known as a memory leak on systems without garbage collection.
If you originally set 1000 references to null, then you have space for just 1000 references, typically 1000 * 4 bytes (on a 32-bit system, twice that on 64). If those 1000 references originally pointed to real objects, then you allocated 1000 times the size of each object, plus space for the 1000 references.
In some languages (like C and C++), pointers always point to something, even when "uninitialized". The issue is whether the address they hold is legal for your program to access. The special address zero (aka null
) is deliberately not mapped into your address space, so a segmentation fault is generated by the memory management unit (MMU) when it is accessed and your program crashes. But since address zero is deliberately not mapped in, it becomes an ideal value to use to indicate that a pointer is not pointing to anything, hence its role as null
. To complete the story, as you allocate memory with new
or malloc()
, the operating system configures the MMU to map pages of RAM into your address space and they become usable. There are still typically vast ranges of address space that are not mapped in, and so lead to segmentation faults, too.
Best Answer
The macro
NULL
is a null-pointer constant and has either an integer type or a pointer type.Using
NULL
to assign or initialize a non-pointer variable will lead to question marks from other programmers at the least and it might result in compiler failures.A line like
is not considered good code and it will make the code less readable.