C ++ pointer is passed as a function parameter [C ++]

Transfer from: http://blog.csdn.net/fjb2080/article/details/5623427

 

Original article, please indicate the source for reprinting, thank you!
Author: Lin Qing, blog name: Du Jing fly empty

 

Blog address: http://blog.csdn.net/fjb2080

 

In fact, for C or C ++, the hardest part is estimated to be the pointer. The pointer is powerful, but it is also a place where a lot of people carry it.

 

 

Some time ago wrote an article " C  arrays and pointers ++ Similarities and Differences  " for C and C ++ pointers made a preliminary explanation. This time I will explain the problem of passing pointers as function parameters.

 

Many people are familiar with the use of pointers, but they are often included in the problem of pointers because they do not understand the nature of pointers. In fact, if you understand the nature of pointers, the use of pointers will be clear at a glance.

 

As a beginner in C, I often encounter two classic problems of passing pointers as function parameters. Here, I will explain these two problems through the essence of the pointer, so no matter what kind of pointer problem you encounter in the future, if you analyze the pointer in this way, it may be solved!

 

First, the first question is this:

Write a function to exchange the values ​​in the two parameters.

 

Beginners often write:

 

void exchange(int x, int y)

{

int p=x;

x = y;

y = p;

}

 

After that, you will find information to understand that it should be written like this:

void exchange(int *x, int *y)

{

int *p=x;

* x = * y;

*y = *p;

}

 

The second problem is to write a function that allocates memory to a pointer:

Beginners often write this way:

void my_malloc(void* p, int size)

{

p = malloc(sizeof(int)*size);

}

 

Then I checked the information and knew that it should be written like this:

void my_malloc(void** p, int size)

{

*p = malloc(sizeof(int)*size);

}

 

Although there are many such discussions on the Internet, and many people have done a lot of explanations, they have never been able to give a statement that is clear and can be remembered for a long time. A principled understanding!

 

First of all, you must remember that  pointers, like variables, have addresses, but the value of the variable is interpreted as a value, and the value of the pointer is interpreted as an address.

 

Below, we look at the code:

void main()

{

int x;

int *p;

}

 

We look at the memory structure of this function:

 

This is a stack structure of a function, we can see that both variables and pointers occupy 4 bytes. Moreover, because we have not initialized them, the contents of the variable x and the pointer p are random, that is to say, the value of x is uncertain, p may point to a certain memory address, if you now operate on p may cause program crash.

 

In fact, we remembered that the pointer also has  the concept of address , and many problems were solved.

 

Next, let me analyze the situation where the pointer is passed as a function parameter.

If our code looks like this, what will you see:

 

int main(int argc, char* argv[])

{

int *a = new int(10);

func(a);

 

return 0;

}

 

The first thing to say is of course: pointers also have addresses.

The second thing to say is: when passing a variable to a function parameter, this variable is copied.

 

For the second point, we should understand when we understand that the void exchange (int x, int y) function wants to exchange the values ​​of these two variables.

E.g:

int a;

int b;

exchange(a,b);

The values ​​of a and b cannot be exchanged, because at this time a and b in exchange (a, b) are not the original a and b variables, they are just copied.

 

With these two concepts, it is not difficult to understand the problem of passing pointers as function parameters.

 

First, let's look at the memory structure of a pointer and p pointer in the above code.

We see that when we use a as the parameter of the func function, the function copies the pointer, but the contents of the two pointers are the same, that is, they point to the same memory, namely 10.

 

If you do n’t understand it, I will explain it through a piece of code and test:

 

 

[cpp]  view plain  copy
 
 
  1. #include <stdio.h>  
  2. void func(int* p)  
  3. {  
  4.     printf("*p = %d/n", *p);  
  5.     printf("&p = %p/n", &p);  
  6. }  
  7. int main(int argc, char *argv[])  
  8. {  
  9.     int *a = new int(10);  
  10.     printf("*a = %d/n", *a);  
  11.     printf("&a = %p/n", &a);  
  12.     func(a);  
  13.     return 0;  
  14. }  

 

 

 

Compile: g ++ -g -Wall test1.cpp

Run: ./a.out

Output:

*a = 10

&a = 0xbfd4447c

*p = 10

&p = 0xbfd44460

 

We see the output, the value of the address pointed by a is the same as the value of the address pointed by p, both are 10. However, for pointers a and p, their own addresses are different, so we see that function func copies pointer a to p. They have the same value, but have different addresses and are different pointers.

 

Let's go further:

 

[cpp]  view plain  copy
 
 
  1. #include <stdio.h>  
  2. void func(int* p)  
  3. {  
  4.     printf("*p = %d/n", *p);  
  5.     printf("&p = %p/n", &p);  
  6.     printf("&*p = %p/n", &*p);  
  7. }  
  8. int main(int argc, char *argv[])  
  9. {  
  10.     int *a = new int(10);  
  11.     printf("*a = %d/n", *a);  
  12.     printf("&a = %p/n", &a);  
  13.     printf("&*a = %p/n", &*a);  
  14.     func(a);  
  15.     return 0;  
  16. }  

 

 

 

Compile output:

*a = 10

&a = 0xbfe1c77c

&*a = 0x94b6008

*p = 10

&p = 0xbfe1c760

&*p = 0x94b6008

 

We can further see that the address value of a pointer is the address and the pointer value p points to is the same, are 0x94b6008, just as shown above, in order to deepen the impression, then look at this chart  , then again Compare the program output  , and then experience the two points I mentioned above  . One point is: the pointer has an address  . Another point is: the parameters of the function are copied in the past  .

 

 

 

Speaking of which, let's return to the two problems mentioned at the beginning of the article, one is the exchange problem:

 

void exchange(int *x, int *y)

{

int *p=x;

* x = * y;

*y = *p;

}

 

So why can this be exchanged:

int a = 2;

int b = 3;

exchange(&a, &b);

 

When we passed the addresses a and b to the exchange function, the function copied the two addresses and assigned them to the two pointers x and y. These two pointers point to the variables a and b. Their graphs are as follows:

 

So, when we dereference pointers:

int *p=x;

* x = * y;

*y = *p;

 

We operate on the values ​​of the variables in a and b, so we exchanged the values ​​of a and b successfully.

 

Let us look at the second question:

void my_malloc(void* p, int size)

{

p = malloc(sizeof(int)*size);

}

When this is the case:

int *a;

my_malloc(a, 10);

Why does this fail!

 

Next, let me analyze:

When we call my_malloc (a, 10); function, and the function has not been executed until p = malloc (size); statement, the situation is like this:

 

We see that the values ​​of the pointers of a and p are the same, both point to an indefinite address.

At this time, we execute this statement:

p = malloc(sizeof(int)*size);

Let's divide this statement into two parts. One is to execute malloc (sizeof (int) * size) first, then execute the assignment statement, and pay the return value of malloc (sizeof (int) * size) to p.

The first step: first execute malloc (sizeof (int) * size); (Here we only consider the successful allocation of memory by malloc)

 

Step 2: Pay the return value of executing malloc (sizeof (int) * size) to p, as shown below:

 

From the above picture, we can know that this is why we still cannot assign an address to a.

 

Let's analyze this below:

void my_malloc(void** p, int size)

{

*p = malloc(sizeof(int)*size);

}

 

int *a;

my_malloc(&a , 10);

Why do you succeed in this way!

 

 

We see that when the function is executed

my_malloc(void** p, int size);

But it has not been implemented

*p = malloc(sizeof(int)*size);

Statements, their memory structure diagram is as follows:

 

In fact, here, we can treat two-dimensional pointers and one-dimensional pointers as variables, and they also have addresses. It's just that its explanation is different.

Variable: The value inside is a numeric value.

One-dimensional pointer: The value inside is an address, and the value in this address is a numeric value.

Two-dimensional pointer: The value inside is an address, and the value in this address is also an address.

 

So, I looked at the picture to explain p:

p is an address, this address is & a, which is the address value of the pointer a, and the value in the address of the pointer a is also an address. This address points to an uncertain place. Understanding will be good!

 

The figure after executing malloc (size) is as follows:

 

Then execute the assignment statement:

*p = malloc(sizeof(int)*size);

After, as shown below:

 

Then, we allocated memory to pointer a successfully.

 

The pdf download address of this article: The problem of c ++ pointer passing as a function parameter.pdf

Published 36 original articles · praised 17 · visits 6274

Guess you like

Origin blog.csdn.net/qq_39248307/article/details/79779670