The difference between a reference parameter and a pass parameter is deeply resolved

  • 2020-04-02 01:22:21
  • OfStack

1. As we all know, the ordinary pass-through parameter is passed from the line parameter to the argument;

The compiler generates a temporary variable for each argument inside the function, pushes each argument onto the stack, and stores the argument's value in the temporary variable.

Therefore, in the low-end 8-bit machine, there is a relatively strict limit on the number of parameters, because the depth of the stack is very limited; Of course, there is the same problem on the general machine, too many parameters are not appropriate, but the general machine stack is much deeper than the low-end machine;

2. The reference parameter is the address of the variable;

You're essentially passing a pointer to a variable;

The advantage of using references as parameters is that they reduce the memory overhead of temporary variables, so they are often used to operate on large objects.

And often referred to as the parameter is to protect the parameter is not modified;

Attachment: knowledge of stacks and stacks
The memory used by a c/ c ++ compiled program is divided into the following sections

1. Stack contents     The compiler automatically allocates the release, storing the parameter value of the function, the value of the local variable, and so on. It operates like a stack in a data structure.

2. The contents of the heap area     Release is usually allocated by the programmer, if the programmer does not release, the end of the program may be recovered by the OS. Note that it is not the same as the heap in the data structure, but is allocated like a linked list.

The storage of global variables and static variables is put in the same place. The initialized global variables and static variables are in the same area. The uninitialized global variables and uninitialized static variables are in the adjacent area. - system release after program completion

4. Text constant area   This is where the wok constant string is placed. When the program is finished, it is released by the system

5. The program code area contains the binary code of the function body.


Related articles: