Understanding of level 2 pointers in C/C ++

Source: Internet
Author: User

In the past, when I learned the data structure, I never understood how to pass ** P and the meaning of "use &" in the function parameters. I copied a short section here.ArticleEasy to understand. When you don't understand, let's look at these paragraphs.Code.

How does the pointer Parameter Pass the memory?

If the function parameter is a pointer, do not expect this pointer to apply for dynamic memory. See the following example:

Void getmemory (char * IP, int num)

{

IP = (char *) malloc (sizeof (char) * num );

}

Void test (void)

{

Char * STR = NULL;

Getmemory (STR, 100); // STR is still null

Strcpy (STR, "hello"); // running error

}

 

Try to apply for dynamic memory with pointer Parameters

The fault lies in the getmemory function. The compiler always needs to make a temporary copy for each parameter of the function. The IP address copy of the pointer parameter is _ IP, and the compiler makes _ IP = IP. IfProgramThe content of the parameter IP address is modified accordingly. This is why pointers can be used as output parameters. In this example, _ IP applies for a new memory, but changes the memory address indicated by _ IP, but the IP address remains unchanged. Therefore, the getmemory function cannot output anything. In fact, each execution of getmemory will leak a piece of memory, because the memory is not released with free.

If you need to use the pointer parameter to request memory, you should use "pointer to Pointer", as shown in the following example:

Void getmemory (char ** P, int num)

{

* P = (char *) malloc (sizeof (char) * num );

}

Void test (void)

{

Char * STR = NULL;

Getmemory (& STR, 100); // note that the parameter is & STR, not Str

Strcpy (STR, "hello ");

STD: cout <STR <STD: Endl;

Free (STR );

}

 

Here is a piece of understanding: This program is to use the function to allocate a bit of memory to the STR pointer-malloc, and then assign a value to the STR pointer... Note that this is done by changing the pointer of STR inside another function (STR originally points to null and now points to the memory area allocated in the function getmemory). Therefore, the second-level pointer should be used for implementation, the essence is to change the direction of the STR pointer (for example, from pointing to A to B), rather than changing the content of the STR pointer pointing to the address.

 

 

Apply for dynamic memory with pointer to pointer

Of course, we can also use function return values to transmit dynamic memory. This method is simpler, as shown in the following example:

Char * getmemory (INT num)

{

Char * IP = (char *) malloc (sizeof (char) * num );

Return IP;

}

Void test (void)

{

Char * STR = NULL;

STR = getmemory (100 );

Strcpy (STR, "hello ");

STD: cout <STR <STD: Endl;

Free (STR );

}

 

We certainly hope to use the creatlink (...) function to add linked list nodes when we are doing a linked list. There are two ways to achieve this.
First, use a level-1 pointer.

 
Typedef struct node {......} list; node * Create (list * l) {list * head; head = L; malloc... // apply for memory space for the node ...... // operation return head;} int main (...) {...... list * listhead createlist (listhead );.... // In any future operations, we should consider whether we get the head pointer of the linked list and which one is the head pointer of the linked list wave, do we need to restore urn to return the linked list header pointer ?? .... }

 

This can achieve the purpose of deleting and adding nodes. However, under any circumstances, we have to seize the header pointer, that is, after adding and deleting nodes, for any modification to the length of a linked list, we need to return the head pointer of the linked list, that is, return head. Therefore, we need to use this function to obtain the head pointer, capture it, and hold it, then, perform the operation.

Method 2: Use a double pointer, that is, a second-level pointer.

Typedef struct node {......} list; void create (list ** L) {list * head; head = * l; // I do not know if it is written as * l. The original text is written as l, I think it should be * l malloc... // apply for memory space for the node ...... // operation} int main (...) {...... list * st createlist (ST )...... // In any future operations, whether it is delete or insert, we do not need to consider whether we have returned head or not. In any case, you only need to use st to perform operations on the linked list, because st is the head pointer of the linked list and remains unchanged, because an address space has been allocated to St during the declaration, it exists until the end of the main function}

 

 

 

Summary: If a pointer is passed in a function parameter and you want to change the pointer, you can use a second-level pointer. (Of course, if it is not in the function, you can directly assign a value to the first-level pointer to point to another data)

 

Related Article

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.