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

I was wondering when we use a pointer to a pointer in C ++ and why do we need to point to a pointer? I know that when we point to a pointer, it means that we store the memory address of the variable in memory, but I don’t know why we need it? I also saw some examples that always use a pointer-pointer when creating the Matrix ! But why might Matrix need a pointer to a pointer?

+6
source share
6 answers

If you want to change the value of the variable passed to the function as an argument to the function and save the updated value outside this function, you need a pointer (one pointer) to this 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 the function as an argument to the function, you need a pointer to a pointer.

In simple words, use ** if you want to save (OR save the change) the allocation or assignment of memory even outside of a function call. (So, pass such a function with a pointer with two pointers.)

This may not be a very good example, but it will show you the main 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; } 
+1
source

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

I would say that it’s better never to use it in C ++. Ideally you will use only when working with C APIs or some legacy materials that are still associated with or are developed using the C APIs.

Pointer to a pointer is largely deprecated from the functions of the C ++ language and the accompanying standard library. You have links to when you want to pass a pointer and edit the source pointer in a function, and for things like a pointer to an array of strings, you'd better use std::vector<std::string> . The same goes for multidimensional arrays, matrices, and something else, C ++ has a better way to handle these things than critical pointers to pointers.

+6
source

You probably saw int main () before, you saw this:

 int main(int argc, char** argv) 

argv is really a double pointer, but in fact it is not a double pointer, but a pointer to an array of pointers, each of which points to an array of characters related to command line arguments.

This is not a good example, since you probably need a more practical example. I will write the best example and edit my post :)

Edit:

If you are familiar with classes and virtual functions, you may also be aware that any class that has a virtual function is automatically assigned the vftp member variable.

The _vftp element is a pointer to a list of all pointers to your virtual functions. It is inserted at the very beginning of the structure. If you created a new object as follows:

 class myclass { public: //void *_vftp; this is where the _vftp member gets inserted automatically virtual void vfunc1(); }; void myclass::vfunc1() {printf("yay");} void main() { myclass *pMyObject = new myclass(); } 

When you create an instance of myclass, _vftp is added to the object structure, and this is the very first variable. Since pMyObject is a pointer to this structure in memory, * pMyObject is eqal for _vftp.

Since _vftp is a pointer to an array of pointers to virtual functions, * _vftp is equal to vfunc1 (function pointer).

This means that if we search pMyObject twice and call it, we will call vfunc1 ():

 typedef (void* (__thiscall* fnVFunc1))(void); ((fnVFunc)**pMyObject)(); 

Although this is not a real use for double pointers, it is a prime example of their use. The most common place for double pointers is hacking and reverse engineering, where you usually need to find the pointer in memory and change everything that it points to.

+1
source

Basically we need a pointer to a pointer when we want to change the address of the pointer that it points to. A very good example would be with a linked list, where we send a pointer to a pointer to the head of the node when we try to insert the value at the beginning. A snippet of the code inserted below.

  int main() { /* Start with the empty list */ struct node* head = NULL; /* Use push() to construct below list 1->2->1->3->1 */ push(&head, 1); push(&head, 2); ..... .... } /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); ..... ..... } 

This is mainly because the pointer was pointing, pointing to memory location 0X100 and we want to change it to point it to another location, for example, 0X108. In this case, a pointer to a pointer is passed.

+1
source

Anytime you are dealing with C libraries. There are two general answers to the same question in C:

Firstly, anytime you want to get a double indexed array, for example:

 int main(int argc, char** argv) 

Secondly, at any time you want to get another return value from the function. There are many functions in libgit2 that do this because they want to return a meaningful error type, not just null , as the first argument to git_branch_create for example.

Of course, you can return two struct elements, but usually these are two additional lines of code. In fact, a pointer to a pointer allows you to write a pointer directly to the structure in which it will live.

In C ++, you avoid using pointers directly when suitable C ++ data types exist, and my libgit2 example is included in C ++ exceptions, but ..

You cannot call C ++ in most high-level languages, so if you are writing a library available in Perl, Python and C ++, then you are writing it in C.

+1
source

Suppose you want to instantiate an object in C ++ ...

 MyClass * obj = new MyClass(); 

You must do this because new returns a pointer to the allocated object in dynamic memory. The following is incorrect:

 MyClass obj = new MyClass(); // wrong. 'new' returns a pointer. 

Say you need an array of objects ...

 MyClass ** objArray = new MyClass*[10]; 
0
source

Source: https://habr.com/ru/post/985883/


All Articles