C++ reference (&) notes
1. The register is generally only 4/8 bytes, so the intermediate variable (temporary variable in the figure below) is not necessarily stored in the register when returning
2. Return by reference can reduce copying and increase efficiency
But running the print will be wrong, because when the stack frame is destroyed, the stack frame will be cleaned up to get a random value
Correct expression:
3. Temporary variables are constant
Such code cannot be compiled, because one is involved here 权限的放大
, because for 引用
, the authority cannot be enlarged, only the authority can be reduced/translated
The temporary variable here a
is constant, so its type is actually a reduction of permissions const double
from const double
conversion to here, so an error will be reported. If it is changed, it can be compiled.int
const int &c=a;
Why are there temporary variables?
When comparing the i
and here , due to the different types, the type needs to be upgraded, usually from small to large. For example, the comparison here must be upgraded to the original variable , and the promotion here is to upgrade the original variable? If the original variable type is modified because of a comparison, isn't it messed up? So a temporary variable will be generated here, and then the type of the temporary variable will be promoted, and then compared with .j
int
double
i
j
4. Is there a space for the reference?
From the grammatical level, it has no open space, it is the alias of the variable, so what about from the perspective of assembly?
First look at pa
the assembly code of the pointer: first store a
the address of a in the register eax
, then eax
give (the address of a stored) to pa
, and then give pa
the address of to the register eax
, [eax]
that is, eax
dereference to and give 1Eh(30)
to eax
.
Then look at the assembly code of the reference, you can see that the operation is almost the same as that of the pointer, so from the bottom layer, the reference has opened up additional space, and the reference is implemented in a similar way to the pointer
Differences between references and pointers:
- A reference conceptually defines an alias for a variable, and a pointer stores the address of a variable.
- References must be initialized when they are defined , pointers are not required
- After the reference refers to an entity during initialization, it refer to other entities , and the pointer can point to any entity of the same type at any time
- There are no NULL references , but there are NULL pointers
sizeof
The meaning is different in : the reference result is the size of the reference type , but the pointer is always the number of bytes occupied by the address space (324 bytes under the platform)
- The self-increment of the reference means that the referenced entity increases by 1, and the self-increment of the pointer means that the pointer offsets the size of a type backward
- multilevel pointers, but no multilevel references
- Entities are accessed differently, pointers need to be explicitly dereferenced, references are handled by the compiler itself
- References are safer than pointers