Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a pointer to pointer is NULL, then is it necessary that the pointer is also NULL?

Tags:

c

null

pointers

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 ?

like image 926
Vikas Rokade Avatar asked Sep 20 '19 06:09

Vikas Rokade


People also ask

Can a pointer to a pointer be 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 when we set pointer to null?

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.

Should I initialize pointer to null?

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 .

IS null pointer same as uninitialized pointer?

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.


Video Answer


4 Answers

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 ...

like image 59
BoBTFish Avatar answered Oct 27 '22 00:10

BoBTFish


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".

like image 30
MSalters Avatar answered Oct 26 '22 23:10

MSalters


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);
like image 45
cocool97 Avatar answered Oct 26 '22 22:10

cocool97


First, there's the usual misconceptions about the mysterious null. In this case there's 3 related terms:

  • null pointer
  • null pointer constant
  • The NULL macro

A 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 type void *, 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.

like image 24
Lundin Avatar answered Oct 26 '22 23:10

Lundin