Summary of differences between value reference and pointer concepts

Source: Internet
Author: User

Concept of passing values and references

Pass by value: this parameter receives the value passed by the real parameter. If it is of a custom type, you must call the copy constructor. The function processes the values of the form parameters.

Pass by reference: The reference is the same object as the original object. If the parameter of a function is a reference, the parameter does not exist between the parameters. The parameter modification in the function is to modify the real parameters because they are reference relations and the same object.

 

Language Application

  • In C language, only one parameter type is passed: pass a value, including passing a common value and pointer.
  • In C ++, two parameter types are passed: pass value and pass reference
  • In java, only one type is passed: pass value. The values in java include: Basic Data Types and objects, where objects are treated as pointers.

The three languages share the same processing method: both common variables and pointers are passed and values are passed. For pointers, check whether the function modifies the pointer value or the value of the object indicated by the pointer.

 

Major differences between references and pointers

(1) The reference must be initialized when it is created, and the pointer can be initialized at any time.

(2) there cannot be a NULL reference. The reference must be associated with a valid storage unit, and the pointer can be NULL ).

(3) once the reference is initialized, the reference relationship cannot be changed, and the pointer can change the object at any time.

 

Basic Theory of function parameter transfer mechanism

In essence, the function parameter transfer mechanism is a problem where the call function (process) and the called function (process) communicate when the call occurs. There are two basic parameter transfer mechanisms: value transfer and reference transfer. The following describes the functions that call other functions as the main functions. The called functions are called functions.

 Passl-by-value processIn, the form parameter of the called function is processed as the local variable of the called function, that is, the memory space is opened in the stack to store the value of the real parameter put in by the main function, it becomes a copy of the real parameter. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function.

  Pass-by-reference ProcessIn, although the parameters in the form of called functions are also used as local variables to open up memory space in the stack, they are stored by the main function.Location of the Real VariableAddress. Any operation of the called function on the form parameter is processedIndirect addressingThat is, access the real variable in the main function through the address stored in the stack. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function.

 

Summary

1. In C ++, passing values (especially for custom types) will lead to overhead for copying constructors, so execution efficiency is lower. It is highly efficient to transmit reference and pointer without executing the copy constructor.

2,When an object is passed or returned by value transfer, the compiler automatically calls the copy constructor!

3. When the pointer is used as a shape parameter, it is best to draw the object pointed to by the pointer. Then, analyze whether the pointer value is modified or the object indicated by the pointer.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.