[C++] Introduction to Basics (3): Quoting Super Complete Arrangement


Table of contents

1. Reference concept

2. Citation properties

3. Referenced usage scenarios

3.1. References as parameters

3.2. Reference as return value

3.2.1. Return by value

​Edit Why should a temporary variable be generated when returning by value?

​​​​​​​​Storage of temporary variables

What does the destruction of memory space mean?

3.2.2. Reference returnsEdit

4. Often cited

 5. The difference between references and pointers

at last



References are used to simplify pointers

Reference and address use the same symbol


1. Reference concept

A reference is not a new definition of a variable, but an alias  for an existing variable in syntax . The compiler will not open up memory space for a reference variable, and it shares the same memory space with the variable it refers to.

Type & reference variable name (object name) = reference entity;

Note: The reference type must be of the same type as the referenced entity


2. Citation properties

  1.  References must be initialized when they are defined
  2.  A variable can have multiple references
  3.  Once a reference refers to an entity, it can no longer refer to other entities (references cannot write linked lists, because the reference point cannot be changed, and nodes cannot be added, deleted, checked, and modified)


3. Referenced usage scenarios

3.1. References as parameters

Advantages of using references as parameters:

  1. Reduce copying and improve efficiency.
  2. It can be used as an output parameter, the formal parameter is modified in the function, and the actual parameter is also modified.

3.2. Reference as return value

3.2.1. Return by value

Please note: there will be an extra layer of copying when returning by value.
 


Why should a temporary variable be created when returning by value?

       ​​​​​​​​​​The above

  If no temporary variable is generated, return n to ret directly , because the function stack frame is destroyed after the function call ends , and the destroyed space is still there, but the right to use this space does not belong to you, and if you visit this location again, it may be Getting the n value may also get a random value, which will cause out-of-bounds access.

​​​​​​​​Storage of temporary

If it is a relatively small temporary variable, it will be stored in a register , because the register is very fast.

But not all temporary variables are like this, because

  1. The number of registers is limited

  2. A single register has only four bytes or eight bytes.

For relatively large temporary variables, space for temporary variables will be opened up in the upper stack frame.

By creating a temporary variable, the destruction of the function stack frame will not affect the space of the temporary variable .

The overall mechanism of the operating system: as long as you return by value, a temporary variable will be generated.

What does the destruction of memory space mean?

  The space is still there, but the right to use it is not ours. The data we store is not protected, and may or may not be destroyed. We can access, but the data we read and write are uncertain​​​​​​.


3.2.2. Reference return

       If it is returned by reference, it can be considered that a temporary variable is also generated, but this temporary variable does not open up space.

       The above figure is an example: the type of the returned temporary variable is int&, and the alias of n is returned. Note that n here is not stored in the stack frame of count but in the static area.
       If this n is in the stack frame of the function count, the alias of n is returned. Since the stack frame of the count function is destroyed at this time, it will cause out-of-bounds access, and the final value obtained is uncertain.

       Note: If the function returns out of the scope of the function, if the returned object is still there (not returned to the system), you can return by reference, if it has been returned to the system, you must return by value.

The meaning of return by reference:

  1. Reduce copying and improve efficiency.
  2. Modify the return value

4. Often cited

Generally, const references are used when passing parameters by reference, which involves permission issues

 About the default problem of passing parameters by reference:


 5. The difference between references and pointers

In the grammatical concept, a reference is an alias, which has no independent space and shares the same space with its referenced entity.

 In the underlying implementation, there is actually space, because references are implemented in the form of pointers.

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 cannot 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.  The meaning is different in sizeof: the reference result is the size of the reference type, but the pointer is always the number of bytes occupied by the address space (4 bytes under the 32-bit platform)
  6.  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

at last

  Yuanzai has finally taken another important step on the road of c++! ! I finally finished quoting, I hope this blog can be helpful to you, and welcome everyone to communicate with me, roll up on National Day! !

 

Guess you like

Origin blog.csdn.net/vpurple_/article/details/127127717