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

后端 未结 6 1638
小鲜肉
小鲜肉 2021-02-13 20:38

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

相关标签:
6条回答
  • 2021-02-13 20:48

    Say you wanna instantiate an object in C++...

    MyClass * obj = new MyClass();
    

    You have to do this because new returns a pointer to the allocated object in dynamic memory. The following would be wrong:

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

    Say you want an array of objects...

    MyClass ** objArray = new MyClass*[10];
    
    0 讨论(0)
  • 2021-02-13 20:55

    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.

    0 讨论(0)
  • 2021-02-13 20:56

    Anytime you're dealing with C libraries. There are two common answers for the same question in C :

    First, anytime you want doubly subscripted array, like :

    int main(int argc, char** argv) 
    

    Second, anytime you want another return value from a function. There are many functions in libgit2 that do this because they wish to return a meaningful error type as opposed to just null, like the first argument in git_branch_create for example.

    You could return a two item struct of course, but that's usually two extra lines of code. In fact, the pointer-to-pointer lets you write the pointer directly into a struct where it'll live.

    In C++, you'd avoid using pointers directly whenever suitable C++ data types exist, and my libgit2 example is subsumed by C++'s exceptions, but..

    You cannot call C++ from most high level languages, so if you're writing a library that you want available in say Perl, Python, and C++, then you write it in C.

    0 讨论(0)
  • 2021-02-13 21:02

    We basically need pointer to pointer when we want to change the address of the pointer it is pointing to. very good example will be the case of linked list where we send a pointer to pointer to the head node when we try to insert a value to the beginning. Snippet of code pasted 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 basically because, say a pointer was initially pointing to a memory location 0X100 and we want to change it to point it to some other location say 0X108. In such case pointer to pointer is passed.

    0 讨论(0)
  • 2021-02-13 21:07

    You have probably seen int main() before, have you seen this:

    int main(int argc, char** argv)
    

    argv is indeed a double pointer, it's not actually a double pointer but it is a pointer to an array of pointers, each pointing to an array of characters pertaining to the command line arguments.

    This is not the best example, as you probably want a more practical example. I will write up a better example and edit my post :)

    Edit:

    If you are familiar with classes and virtual functions then you may also be aware that any class who has a virtual function is automatically given a _vftp member variable.

    The _vftp member is a pointer to a list of all the function 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();
    }
    

    Upon instantiating myclass, the _vftp is added to the object structure and it is the very first variable. Because pMyObject is a pointer to this structure in memory, *pMyObject is eqal to _vftp.

    Because _vftp is a pointer to the array of virtual function pointers, *_vftp is equal to vfunc1 (a function pointer).

    This means if we dereference 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 this is a prime example of applying them. The most common place for double pointers lays in hacking and reverse engineering, where you commonly need to find a pointer in memory and alter whatever it points to.

    0 讨论(0)
  • 2021-02-13 21:08

    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;
    }
    
    0 讨论(0)
提交回复
热议问题