C++ reference (&) notes

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 ais constant, so its type is actually a reduction of permissions const doublefrom const doubleconversion to here, so an error will be reported. If it is changed, it can be compiled.intconst int &c=a;

Why are there temporary variables?

When comparing the iand 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 .jintdoubleij

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 pathe assembly code of the pointer: first store athe address of a in the register eax, then eaxgive (the address of a stored) to pa, and then give pathe address of to the register eax, [eax]that is, eaxdereference 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:

  1. A reference conceptually defines an alias for a variable, and a pointer stores the address of a variable.
  2. References must be initialized when they are defined , pointers are not required
  3. 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
  4. There are no NULL references , but there are NULL pointers
  5. sizeofThe 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 (32

4 bytes under the platform)

  1. 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
  2. multilevel pointers, but no multilevel references
  3. Entities are accessed differently, pointers need to be explicitly dereferenced, references are handled by the compiler itself
  4. References are safer than pointers

Guess you like

Origin blog.csdn.net/AkieMo/article/details/130661739