Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Pointer-to-Pointer in C++?

Tags:

c++

I was wondering when we use Pointer to Pointer in C++ and why we need to point to a pointer? I know that when we point to a pointer it means we are saving the memory address of a variable into the memory but I don't know why we need it? Also I have seen some examples that always the use Pointer-to-pointer in creating a Matrix! But why a Matrix may need Pointer to Pointer?

like image 335
Amir Jalilifard Avatar asked Apr 24 '15 13:04

Amir Jalilifard


People also ask

Why would you use a pointer to a pointer?

One reason is you want to change the value of the pointer passed to a function as the function argument, to do this you require pointer to a pointer. In simple words, Use ** when you want to preserve (OR retain change in) the Memory-Allocation or Assignment even outside of a function call.

Why would you need a pointer to a pointer in C?

Similar to how a pointer variable in C can be used to access or modify the value of a variable in C, a pointer to pointer in C is used to access/modify the value of a pointer variable. Here, the "value" of the former pointer is as usual a memory address.

Can you have a pointer to a pointer?

A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.

Which is used for pointer to Pinter?

We already know that a pointer points to a location in memory and thus used to store the address of variables. So, when we define a pointer to pointer.


2 Answers

When you want to change the value of variable passed to a function as the function argument, and preserve updated value outside of that function, you require pointer(single pointer) to that variable.

void modify(int* p)
{
  *p = 10;
}

int main()
{
  int a = 5;
  modify(&a);
  cout << a << endl;
}

Now when you want to change the value of the pointer passed to a function as the function argument, you require pointer to a pointer.

In simple words, Use ** when you want to preserve (OR retain change in) the Memory-Allocation or Assignment even outside of a function call. (So, Pass such function with double pointer arg.)

This may not be a very good example, but will show you the basic use:

void safe_free(int** p) 
{ 
  free(*p); 
  *p = 0; 
}

int main()
{
  int* p = (int*)malloc(sizeof(int));
  cout << "p:" << p << endl;
  *p = 42;
  safe_free(&p);
  cout << "p:" << p << endl;
}
like image 148
CreativeMind Avatar answered Sep 16 '22 17:09

CreativeMind


When to use Pointer-to-Pointer in C++?

I'd say it is better to never use it in C++. Ideally, you will only have to use it when dealing with C APIs or some legacy stuff, still related to or designed with C APIs in mind.

Pointer to pointer has pretty much been made obsolete by the C++ language features and the accompanying standard library. You have references for when you want to pass a pointer and edit the original pointer in a function, and for stuff like a pointer to an array of strings you are better off using a std::vector<std::string>. The same applies for multidimensional arrays, matrices and whatnot, C++ has a better way of dealing with those things than cryptic pointers to pointers.

like image 43
dtech Avatar answered Sep 18 '22 17:09

dtech