Simple insights on first-level pointers and level two pointers

Source: Internet
Author: User

First-level pointer form such as: int *p
Second-level pointer form such as: int **p

Perhaps a lot of beginners will wonder if the function parameter should use a first level pointer or a level two pointer.


Here are a few examples to illustrate:

The first one is a list that everyone is familiar with.

Here is a list structure:

typedef struct NODE
{
int value;
struct Node *next;
}


For the function to add data to the tail of the linked list, the passed in parameter must be a level two pointer.

The assignment action that occurs when the parameter is passed is:

Node *hnode;
Node **pnode = &hNode;

Pnode points to the Hnode address, so the *pnode effect is equivalent to Hnode.
You can change the point of the Hnode and the value of the memory pointed to.
So what happens if the parameter uses a first-level pointer.


The assignment action that occurs at this time when the parameter is passed is:
Node *hnode;
Node *pnode = Hnode;

Parameter Pnode both point to the same piece of memory at this time and hnode (this memory may be null)
at this point, a new node is added to the tail of the linked list, and the linked list is null at this point.
adding the Post parameter Pnode points to a new piece of memory, while Hnode still points to the original memory (that is, null)
when the function returns, Hnode still points to null, and the parameter pnode as a local variable is automatically destroyed, and the memory it points to is dynamically allocated.
so there is no local pointer pointing or being released, so a memory leak occurs.
so you have to use a level two pointer here.


Here's a more common example.
Defines a function that assigns the specified memory to a shaping pointer (initially null).
Error procedure:
void Mallocmem (int *p,int size)
{
p = new Int[size];
}

Many people will thinkA function parameter is a pointer to the original pointer directly into the function to use, in fact, even if the parameter is a pointer, will also occur replication.
I also said on the list above that if we were to call Mallocmem in the main function to pass a pointer int *s; Then an assignment action occurs at the function call,
p = s; So in fact P and s are two pointers, except that they point to the same memory.
p = new Int[size]P is tantamount to abandoning s pointing to a new memory, when the function returns,s still pointing to the original place (NULL). Also, a memory leak can occur.

The right thing is
void Mallocmem (int **p,int size)
{
*p = new Int[size];
}

Several other linked list operators that must use a level two pointer also have: delete nodes.
Therefore, any operation that involves a dynamic allocation of memory that would cause the pointer to occur (new delete) must be secured with a level two pointer.

What's left is a list function (traversal, merge, and so on) that does not have to use level two pointers.

To put it simply: A level pointer can do something level two pointers can do, or vice versa.
When you are using it, think about what you want to do with the pointer.

Simple insights on first-level pointers and level two pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.