The incoming parameter of a C + + function is a pointer to a pointer (* *)

Source: Internet
Author: User

To modify the value of a variable, you need to use a pointer to the variable type as a reference to the argument or variable. If the variable is a generic type of variable, such as int, you need to use a pointer of type int int * As the argument or the reference type of int int&. However, if the variable type is a pointer type, such as char*, you need to use a pointer of that type, that is, pointer-to-pointer-type char* *, or reference-type char*& of the type.

The first thing is to be clear whether the pointer or value passed in the function will create a copy, the function after the end of the value can not be passed out because of the value of the copy, and the value passed is not modified, the pointer can be passed out because we are modifying the pointer to the content rather than the address pointed to by the pointer.

Since we are going to give an example, we will find a practical example that is more classical.

In our memory management, if you want to create a function that allocates space, the malloc method is called in the function to request a memory area.

Let's start with an example of the error, as follows:

void GetMemory1 (char *p,int num)

{
P=malloc (sizeof (int) *num);

Return

}

void Test1 ()

{

Char *p=null;

GetMemory (P);

}

The above example is very common to apply a pointer as a parameter to a dynamic memory space, but this program is wrong.

Cause of the error:

Since *p is actually a copy of P in Test1, the compiler always makes a temporary copy of each parameter of the function. In this example, P has applied for new memory, but only the memory address pointed to by P has changed, but P has not changed in Test1. Because the function GetMemory1 has no return value, p in Test1 does not point to the requested memory.

Because malloc works by looking for a block of available memory in the heap, it returns a pointer to the allocated memory.

So at this point P points to the address of the requested memory. Because a copy pointer is created in the function body when the pointer is passed as an incoming parameter _p

The _p pointer is linked to the P-pointer in that they point to the same memory area, but malloc's function allows _p to point to another area of memory, and this memory area does not have a seat-out parameter passed to P,

So p doesn't change any more, it's still null.

How can we solve these problems?

The following methods can be used to solve the problem:

void GetMemory2 (char **p,int num)

{
* P=MALLOC (sizeof (int) *num);

Return

}

void Test2 ()

{

Char *p=null;

GetMemory (&P);

}

Here's how to start analyzing the principles of GetMemory2 () and Test2 ():

Char **p can be split (split from left to right) char* *p, so you can think of *p as a pointer to a char * (note that this is not p but *p). Then P is the address of a pointer to char*, in other words p is a pointer to this char* pointer.

From test2 you can see that P is a char* pointer, &p is the address of this char* pointer, in other words &p is a pointer to this char*p pointer, and the GETMEMORY2 () to match the definition. So use &p instead of p when calling.

In GetMemory2 () *p=malloc (sizeof (int) *num);

Where *p holds the address of the allocated memory, p is a pointer to the allocated memory address.

In fact, the reason why you should use pointers is simple:

Because the VC internal mechanism is to make a copy of the function's incoming parameters, if we pass in to get malloc allocated memory address of the copy changes, and our parameters are not synchronized, unless the use of the function return value of the way to be outgoing.

So we're going to find a constant parameter that can and can be used to get the malloc memory address,

If you can create another pointer a, this pointer points to GetMemory1 (char * P,int ...). ) in the passed parameter pointer p, then even if the content of *p is changed, and the address of P is not changed, we can still use this pointer a to get the address of P and get the allocated memory address that *p points to, yes this pointer A is the pointer to the (char *) pointer (char * *) that this article wants to speak.

So we created a char * pointer *p (Note that this is not a char * pointer p), this p as an incoming parameter, after entering the function, the system will create a copy of the pointer _p, we let *_p point to malloc allocated memory address (note this is *_p, not _p), _ P acts as a pointer to the allocated memory address pointer, so that _p does not change during the allocation process.

Also note that the P of Char *p in void Test2 () is pointing to the address of the memory allocated by malloc, which is returned by the GetMemory2 () function after &p as an incoming parameter, and the P pointer points to the actual allocated memory space by &p.

Finally, it is important to note that void Test2 () in GetMemory (&p), and void GetMemory2 (char **p,int num) Define this function, it is easy to have a confusion, The variables at the point of use and the variable at the definition are how one by one corresponds.

Here's an explanation:

does not correspond to the relationship: In fact, these two P is not a p,&p in the P is a char * pointer, and char**p in the p is a pointer to the char * pointer.

Correspondence: In fact, this correspondence is in the Void Test2 () call to pass in the parameter &p is a pointer to the char * pointer, the GETMEMORY2 () definition of char **p is also a pointer to the char* pointer, so said Arguments passed in at the time of invocation and incoming parameters used at the time of definition must be matched.

If you look at the above text or is more chaotic, use a sentence to summarize the following points:

The arguments passed in the 1,VC function mechanism are either a pointer or a value, if the value A is directly created another value B, the value B has the same value as a, and if the incoming parameter pointer C creates a copy of the other pointer D, then d points to the same address as C.

2, the 1th summarizes that the function of the pointer-to-parameter ratio of the function is to modify the contents of the address pointed to by pointer d to tell the pointer C, because C and D point to the same address.

3, the 2nd Hop summary of Pointers C and D are common to the pointing address can be a value type, or it can be the address of another pointer (that is, the above-mentioned * *).

The incoming parameter of a C + + function is a pointer to a pointer (* *)

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.