Consider I have a structure
typedef struct point_t
{
int x;
int y;
}POINT;
I create a pointer to pointer for POINT and initialize it to NULL
.
POINT **ppPoint = NULL;
Should *ppPoint
also return NULL
?
Hence when a pointer to a null pointer is created, it points to an actual memory space, which in turn points to null. Hence Pointer to a null pointer is not only valid but important concept.
What happens if you set the pointer to NULL before freeing the memory? If you try to free a null pointer, nothing will happen. If there are other pointers to the same memory, they can continue to be used to reference and eventually free the memory.
No, you don't have to set it to NULL , but some consider it good practice as it gives a new pointer a value that makes it explicit it's not pointing at anything (yet). If you are creating a pointer and then immediately assigning another value to it, then there's really not much value in setting it to NULL .
A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, depending on the language and implementation, an uninitialized pointer may not have any such guarantee.
The fact that there are 2 layers of pointers here is actually irrelevant. The fact that your outer pointer in NULL
means it is illegal to dereference it. Therefore it is not meaningful to ask what value you would get by dereferencing it.
Consider this:
int *p = NULL; // modern practice would be to use nullptr anyway
if (0 == *p) // this is Undefined Behaviour!
{
// do something, maybe?
}
You are just adding another layer to this, where instead of int
, you have point_t*
, but it really makes no difference.
The thing with Undefined Behaviour is that anything can happen! Your program might crash, or it might appear to work, or it might give you garbage values sometimes, or it might ...
A null pointer does not point to anything. That applies to ordinary pointers, function pointers and member pointers alike. It also applies to pointers to pointers.
Therefore, it doesn't make sense to talk about the value of "what it points to".
You can't dereference a pointer which has a value of NULL
. So you won't be able to access *ppPoint
.
You can check the address of a variable with the %p
formater in a printf()
to check.
printf("Address of ppPoint: %p", *ppPoint);
printf("Address of *ppPoint: %p", *ppPoint);
First, there's the usual misconceptions about the mysterious null. In this case there's 3 related terms:
NULL
macroA null pointer constant is the integer 0
or that integer cast to a pointer, (void*)0
. The NULL
macro is guaranteed to be a portable null pointer constant.
Whenever you assign a null pointer constant, such as NULL
, to any pointer, that pointer becomes a null pointer. This allows compilers to internally represent a null pointer as something else than 0
, because the address 0x00...00
is quite often a valid physical address on many systems.
Specifically, C17 6.3.2.3/3 defines this:
An integer constant expression with the value
0
, or such an expression cast to typevoid *
, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
When accessing a null pointer by de-referencing it, with don't get any predictable result. This is undefined behavior as per C17 6.5.3.2/4:
If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.
Meaning anything can happen. If you are lucky, you just get a system crash.
You can however compare a null pointer against another null pointer, or against a null pointer constant, and they are guaranteed to be equal (C17 6.5.9).
As for pointer-to-pointer being some a special case, it is not. The pointed-at type is irrelevant for any of the above mentioned rules. Overall there is nothing special with pointer-to-pointer in C - it is never a special case, but always to be regarded as pointer-to-type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With