C++ reference introduction

1. What is a reference?

  For a variable or object alias , the operation on the reference is completely equivalent to the operation on the variable or object bound to it.

2. Referenced characteristics

  • Use the & symbol to declare reference variables. For example: int &b = a;
  • The type of the reference must be the same as the type of the variable or object to which it is bound.
  • It must be initialized while declaring the reference .
  • References are aliases of variables or objects. Therefore, existing reference names cannot be used as names or aliases of other variables or objects.
  • Cannot return references to local variables.

3. Do references occupy memory?

  References are used as aliases for variables or objects. Will memory be allocated separately? Let's explore it through a few examples.
Example 1 The
32-bit execution program is as follows:

int a = 10;
int &b = a;
int c = 10;
cout << "&a = " << &a << endl;
cout << "sizeof(a) = " << sizeof(a) << endl;
cout << "&b = " << &b << endl;
cout << "sizeof(b) = " << sizeof(b) << endl;
cout << "&c = " << &c << endl;
cout << "sizeof(c) = " << sizeof(c) << endl;

The results are as follows:

&a = 00B5F9B0
sizeof(a) = 4
&b = 00B5F9B0
sizeof(b) = 4
&c = 00B5F9B4
sizeof(c) = 4

It can be seen from the execution result that the reference variable b does not occupy memory.
Example 2 The
32-bit execution program is as follows:

int data = 10;
struct A
{
    
    
	int &a;
	int b;
	int c;
	A():a(data){
    
    }
};
struct B
{
    
    
	int b;
	int c;
};
struct A a;
struct B b;
cout << "sizeof(A) = " << sizeof(A) << endl;
cout << "sizeof(B) = " << sizeof(B) << endl;

The results are as follows:

sizeof(A) = 12
sizeof(B) = 8

It can be seen that the reference member variable a in the structure A occupies 4 bytes.
Example 3
will execute the program of Example 2 in 64-bit system as follows:

sizeof(A) = 16
sizeof(B) = 8

It can be seen that the reference member variable a in the structure A occupies 8 bytes.

to sum up:

  • The referenced address obtained in Example 1 is the same as the address of its bind variable, essentially because of the optimization of the C++ compiler, we cannot get the real address of the referenced variable.
  • Examples 2 and 3 prove from another angle that the reference variable occupies the memory space, and the size of the occupied space is the same as the pointer.

4. The essence of reference

  The internal implementation of references in C++ is a ** pointer constant **. That is, the following relationship is established:

Type & name ⇔ \Leftrightarrow Type * const name

note:

  • The C++ compiler uses constant pointers as the internal implementation of references during compilation , so the space occupied by references is the same as the size of pointers .
  • From the perspective of usage, a reference is just an alias, and C++ hides the details of the storage space of the reference for practicality. This is why the reference is the same as the address pointed to by the variable or object to which it is bound.

Let's introduce in detail the process of replacing references inside the C++ compiler:

  • Create a constant pointer b of type* const type.
  • Assign the address of the variable to the constant pointer b.
  • When the C++ compiler finds that there is an operation that requires a value or assignment operation on (reference pointer) b, the C++ compiler will perform a deduction * b operation on b by default -this is the internal behavior of the C++ compiler (so C++ That's why I did a lot of operations invisibly so slowly).

5. Referenced applications

  References are mostly used for parameter passing, because they are similar to pointers, they generally save a lot of memory space and improve the execution efficiency of the program. They are mostly used for the transfer of complex types of variables or objects. For basic type variables when passing parameters, it is not necessary to use references unless necessary, because each reference will occupy a pointer-sized space, which may not save memory in this case.

Reference link:
https://www.cnblogs.com/yr-linux/p/5522047.html
https://www.cnblogs.com/duwenxing/p/7421100.html

Guess you like

Origin blog.csdn.net/qq_39661206/article/details/105121648