The function of secondary pointer

King of heaven and earth tiger 626 2021-03-30 23:33:50 阅读数:584

本文一共[544]字,预计阅读时长:1分钟~
function secondary pointer

One 、 Concept

As follows A Point to B、B Point to C In the pointing relationship of :

First

C yes " A paragraph ", For example, you use malloc perhaps new Allocated a block of memory , And stuff it in " A paragraph ", That's it C 了 .C The starting address of is 0x00000008.

B Is a pointer variable , There is... In it C The address of , however B It also takes up space , therefore B There are also addresses ,B The starting address of is 0x00000004, however B What's in memory is C The address of , therefore B The content is 0x00000008.

So far, it's easier to understand :
 

 
  1.  
    B= 0x00000008; //B The content of
  2.  
    *B = " A paragraph "; //B Quoting , That is to say B The pointer points to C Value
  3.  
    &B = 0x00000004; //B Address fetch ,B The address is 0x00000004

that , Look again. A:

 

A Is a secondary pointer variable , There is... In it B The address of 0x00000004,A There are also addresses , yes 0x00000000;

 
  1.  
    *A = B= 0x00000008; //A Dereference means B The content of
  2.  
    **A = *B = " A paragraph "; //B Quoting , That is to say B The pointer points to C Value
  3.  
    A = &B = 0x00000004; //A Deposit is B The address of ,B The address is 0x00000004
  4.  
    &A = 0x00000000; //A Address fetch

 

The pointer to the pointer, that is, the secondary pointer, holds the address of the primary pointer , such as :

         p Is the first level pointer , What is kept is a The address of ;q It's a pointer to a pointer ( The secondary pointer ), What's saved is the first level pointer (p) The address of ;q The content of is 0xbfaca770,*q The value of is q Point to 0xbfaca776, namely *q It's still an address , That's the pointer p The content of , namely *q=p
 

Two 、 Use

 

The role of secondary pointers as function parameters : Define a pointer outside the function p, Assign a value to a pointer in a function , At the end of the function, the pointer p take effect , So we need a secondary pointer .

 

Take a look at the following code : There are two variables a,b, The pointer q,q Point to a, We want to make q Point to b, It's implemented in a function .

1. Let's take a look at the implementation of the first level pointer

#include<iostream>
using namespace std;
int a= 10;
int b = 100;
int *q;
void func(int *p)
{
cout<<"func:&p="<<&p<<",p="<<p<<endl; //note:3
p = &b;
cout<<"func:&p="<<&p<<",p="<<p<<endl; //note:4
}
int main()
{
cout<<"&a="<<&a<<",&b="<<&b<<",&q="<<&q<<endl; //note:1
q = &a;
cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl; //note:2
func(q);
cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl; //note:5
system("pause");
return 0;
}
What's wrong with that ? Why? *q It's not equal to 100? Let's take a look at the output :
&a=0032F000,&b=0032F004,&q=0032F228
*q=10,q=0032F000,&q=0032F228
func:&p=0018FD24,p=0032F000
func:&p=0018FD24,p=0032F004
*q=10,q=0032F000,&q=0032F228
We look at the output :
note:1->a,b,q They all have an address .
note:2->q Point to a.
note:3-> We found that the parameters p The address of has changed , Follow q It's different. , Yes, parameter passing makes a copy , That is to say p and q Not the same pointer , But it's pointing to an address 0x0032F000(a The address of ) It's still the same .
note:4->p Point back to b.
note:5-> Exit function ,p It's not going to change q Impact .
Conclusion :
The compiler always makes a temporary copy of each parameter of a function , Pointer parameter p A copy of is p, The compiler makes p = q( however &p != &q, That is, they are not in the same memory address , It's just that their content is the same , All are a The address of ). If the program in the function body changes p The content of ( For example, here it points to b). In this case ,p Applied for new memory , Just put p The memory address is changed ( Turned into b The address of , however q The memory address pointed to has no effect ), So it doesn't affect pointers outside the function here q.
This requires two pointer operations :
2. Secondary pointer operation
#include<iostream>
using namespace std;
int a= 10;
int b = 100;
int *q;
void func(int **p) //2
{
cout<<"func:&p="<<&p<<",p="<<p<<endl;
*p = &b; //3
cout<<"func:&p="<<&p<<",p="<<p<<endl;
}
int main()
{
cout<<"&a="<<&a<<",&b="<<&b<<",&q="<<&q<<endl;
q = &a;
cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl;
func(&q); //1
cout<<"*q="<<*q<<",q="<<q<<",&q="<<&q<<endl;
system("pause");
return 0;
}
There are only three changes here , It becomes a secondary pointer . We'll see :
Because it passed the pointer q The address of ( The secondary pointer **p) To the function , So the secondary pointer copy ( The copy is p, The copy in the first level pointer is q That's why there's a problem ),( The pointer is copied, but the pointer content, that is, the address the pointer points to, is unchanged ) So it still points to the first level pointer q(*p = q). Here, no matter how many copies , It still points to q, that *p = &b; The natural thing is q = &b; 了 .
3. Let's do another example :
In our code, it is more common to take the secondary pointer as a parameter , Defines a pointer MyClass *ptr=NULL, Assign a value to a pointer within a function *ptr=malloc(...), The pointer is still valid at the end of the function . At this time, we must use the secondary pointer as the parameter func(MyClass **p,...), Why can't the primary pointer work? It says .
void my_malloc(char **s)
{
*s=(char*)malloc(100);
}
void main()
{
char *p=NULL;
my_malloc(&p);
//do something
if(p)
free(p);
}
Here's the pointer p Allocate memory ,do something, then free(p), If you use one pointer , So it's equivalent to giving a p A copy of the s Allocate memory ,p Still no memory allocated , After using the secondary pointer , That's right p Allocated memory . 
版权声明:本文为[King of heaven and earth tiger 626]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/03/20210326101342253A.html