A brief analysis of C++ references and const Pointers and various transfer methods

  • 2020-05-27 06:33:46
  • OfStack

A brief analysis of C++ references and const Pointers and various transfer methods

First of all, we know that const int *p and int const *p are the same, that is, *p is constant; int * const p is not the same as above, that is, p is constant; We know that a reference is just a single name, shares storage space with a variable, must be initialized at the time of definition, and can no longer be an alias for another variable. What does this remind us of? It looks like the property of int * const p.

The underlying reference is implemented using the const pointer. Here's a quick example:


#include <iostream>
using namespace std;

void swap(int &x, int &y)
{
  int temp = x;
  x = y;
  y = temp;
}

void swap(int *const x, int *const y)
{
  int temp = *x;
  *x = *y;
  *y = temp;
}

int main(void)
{
  int a = 5;
  int b = 6;
  swap(a, b);
  cout << "a=" << a << " b=" << b << endl;
  int c = 7;
  int d = 8;
  swap(&c, &d);
  cout << "c=" << c << " d=" << d << endl;
  return 0;
}

In fact, the two swap functions achieve the same effect (name mangling), while const references const int & We can also use the analogy const int * const p where you can neither be a reference to another variable nor change the value of a variable by reference.

References are often passed as arguments to functions, and can be compared with value passing and pointer passing:

Value passing: to allocate space when the argument initializes the parameter, copy the argument content to the parameter

Reference passing: the argument initializes the parameter without allocating space

Pointer passing: essentially value passing, but if we want to modify the pointer itself, we can only use the pointer of the pointer, namely **, or pointer reference * &

And one of the less safe things about using Pointers is that many people forget to add the const restriction, which means that it's very likely that you're going to use this pointer to another variable later in the program, which is confusing.

When using a reference as a function return value, be sure not to return a reference to a local variable. Here's a small example:



#include <iostream>
using namespace std;


int &add(int a, int b)
{
  int sum;
  sum = a + b;
  return sum;
}

int main(void)
{
  int n = add(3, 4);
  // cout<<"just test"<<endl;
  int &n2 = add(5, 6);
  cout << "n2=" << n2 << endl;
  cout << "n=" << n << endl;
  return 0;
}

In the example above we returned a reference to a local variable, so what is the output?


n2=11
n=7

I think that's true, right? Let's try it again. Let's add one statement at the end and print 1 n2


cout<<"n2="<<n2<<endl;
n2=11
n=7
n2=1474313670

I wonder why this print is so big and we haven't changed the value of n2 at all. Don't let it fool you. This is what happens when you return a reference to a local variable.

Function actually returns the local variable sum references, and n2 itself and reference, which refer to the original sum have piece area, the first printing failed because originally written in the values on the 11 sum area has not yet been covered, and to run after the two print statements to print again, probably belong to original sum area became dirty, be covered with other uncertain value, each print is not a constant value.

What about n? For n, even if you print 1 last, n is still equal to 7, because n itself is a variable. When the function returns, it immediately saves the value of the region where sum is located. Unless you change n, n will not change in the main function stack until the function exits and the variable is released. It should be clear that local variables are released on the function stack, but the value of the original area at the first time is still the original value, but after the program runs, the stack memory area reuse, 1 is overwritten.

The above is C++ reference and const pointer and various ways of transmission, if you have any questions, please leave a message or to the community of this site to exchange discussion, thank you for reading, hope to help you, thank you for your support of this site!


Related articles: