Nature of the reference type
How does the compiler implement the reference type?
Prerequisites 1: Use const to define Constants
Const can be used to define constants in a way similar to defining variables. During definition, constants must be initialized to specify constants. For example, const int a = 1 ;.
Will the compiler allocate memory space to constants defined by const? If the memory space is allocated, the address will be accessed every time this constant is used. Is the space efficiency wasted? You may have such questions at the beginning. In particular, I was born from single-chip microcomputer programming. I felt that the waste efficiency was almost uncomfortable at that time (the Single-chip Computer's computing power was very limited ).
First, whether to allocate space to const constants. The answer is yes. You can obtain the address of a const constant. Therefore, const constants have a place in the memory.
Besides time efficiency, the compiler uses constant folding technology to optimize the code. Specifically, the constant is replaced with the immediate number like the macro replacement. However, unlike macro replacement, this is completed in the compilation phase. In this way, when you use a const constant, you do not need to access the memory to retrieve the constant value, but directly use the immediate number (This number is directly written in the machine command ). This time efficiency is equivalent to macro replacement. Use as many const keywords as possible, which can greatly reduce the number of bugs.
// The C ++ statement and the corresponding disassembly. We can see that space is allocated to a and the initialization is 1 // but the immediate number is 1 when a is used later.
// It does not matter if you do not understand the disassembly. All the information to be expressed in the code has been fully stated in the above.
Const int a = 1; 00C516EE mov dword ptr [a], 1 int B; B = fun (a); 00C516F5 push 1
Prerequisites 2: pointer
Generally, "Pointer" refers to "pointer variable ". However, pointers are sometimes interpreted as synonyms of addresses. Therefore, pointer variables only refer to "a variable, which stores a pointer/address ". Here we will talk about constant pointers and pointer constants.
Constant pointerIt means "a pointer to a constant" (but * ptr is regarded as a constant), which is essentially a pointer variable. The defined method is const int * ptr ;.
Pointer constantIt means "a constant that saves the pointer/address", so the pointer constant is essentially a constant, and the defined method is int * const ptr = & ;. Just as a const constant must be initialized during definition, the constant pointer must also be initialized during definition, that is, the address of the constant.
The previous section describes what is constant folding and how const constants are replaced with immediate numbers. Then the pointer constant will be replaced with the immediate number, but the immediate number represents an address, such as 1480091972 or something.
Prerequisites 3: three attributes of a Variable
There are basically three basic attributes of a variable:
(1) name (description must be displayed)
(2) type (the description must be displayed)
(3) Storage category (default mode or display description (Use: auto, register, static, extern ))
Scopes and lifetime (scopes and lifetime are considered to be two other attributes, but these two attributes are not as basic as the first three attributes) are actually determined by the storage class and the location of the defined variables, therefore, the most basic attributes of a variable are the above three types. When you see a variable in the C ++ source code, qualified programmers should be able to explain what the three attributes of the variable are. For example, a global variable is of the int type and its name is.
However, these attributes only make sense to programmers and compilers. Machines only understand machine codes and do not understand C ++. That is to say, the compiler needs this information when compiling the source code, which is also reflected in the source code, so the compiler can also obtain this information. The compiler compiles based on the information, but the information is lost after compilation, and it is not guaranteed that the information can be obtained from the machine code. Therefore, the name is important to the compiler, but a block of memory does not need a name after compilation, because the memory only needs a number.The name is only important to programmers and compilers.
Reference
The classic interpretation of what is reference is that reference is the alias of a variable. Indeed, this sentence is perfect. If there is any flaw, it is not easy to understand. What is an alias? So why is there a single name for the variable? Especially when the parameter type is reference, how can we take a "name" as a parameter. As mentioned above, the name is only one of the attributes of a variable. If it is just a name, it is not even a variable. What is reference?
I first came into contact with the C language, but I was not very confused about pointers, but the reference of C ++ made me confused for a while. Let's take a look at the reference scenarios:
1 int a = 1; 2 int & B = a; 3 B = 2; 4 cout <a <endl; // output 2
The above code can be used to explain what is "reference is the alias of a variable ". B is the alias of a, that is, the operation on B is the same as the operation on a, B = 2; it can be understood as a = 2 ;. It seems that there is nothing hard to understand. However, the value of the referenced application lies in the reference type and return reference type as the form parameter.
In C language, parameters are passed by values. To modify real parameters, a pointer is required. A typical swap function is implemented as follows:
1 void swap(int*a,int *b){2 int temp = *a;3 *a = *b;4 *b = temp; 5 }
To exchange two variables a and B, enter the address swap (& a, & B); of a and B );. To replace the apples in two boxes, you must first find these two boxes, so the variable address is essential in this algorithm. I think it is natural that I only understand the C language: to modify the value of a real parameter, the address must be input. However, when I access c ++, I find that the same function can be referenced, which is much simpler. Since "the address of a variable is essential in this algorithm", how does the reference implement the incoming address?
ActuallyReference can be regarded as a pointer constant.The const constant must be initialized when defined, and the reference also has the following requirements: when defining a reference, you must specify the reference (the reference type of the parameter is created and initialized when the real parameter is passed in ). Seeing a reference as a pointer constant can explain why the incoming reference is the same as the incoming pointer.
What is an alias? It is an address or an alias.Pointer constant.