Difference analysis of pointers and references in C + + _c language

Source: Internet
Author: User

Conceptually speaking. A pointer is essentially a variable that holds the address of a variable, logically independent, that can be changed, including the change in the address it points to and the data that is stored in the address to which it points.

Whereas a reference is an alias, it is logically not independent and its existence is dependent, so the reference must be initialized at the outset, and the object referenced by it cannot be changed throughout its lifecycle (it can only be attached to the same variable from beginning to end).

In C + +, pointers and references are often used for parameter passing of functions, however, pointer passing parameters and reference passing parameters are essentially different:

The pointer pass parameter is essentially the way the value is passed, and it passes an address value. In the process of value transfer, the formal parameters of the modulated function are treated as local variables of the modulated function, that is, the memory space is opened in the stack to hold the values of the arguments put in by the keynote function, thus becoming a copy of the argument. The characteristic of value transfer is that any operation of the function on the formal parameter is taken as a local variable and does not affect the value of the argument variable of the keynote function.

While in the process of reference transfer, the formal parameters of the modulated function are also used as local variables to open up the memory space in the stack, but at this time the address of the argument variable which is put in by the keynote function is stored. Any operation of the called function on the formal parameter is treated as indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any manipulation of the function on the formal parameter affects the argument variables in the keynote function.

Reference passing and pointer passing are different, although they are a local variable on the stack space of the called function, but any processing of reference parameters will be manipulated to the related variables in the keynote function through an indirection. And for the parameters passed by the pointer, if you change the pointer address in the called function, it will not affect the relevant variables of the key function. If you want to change the related variables in the keynote function by passing pointer arguments, you must use a pointer to the pointer, or a pointer reference.

To further deepen the distinction between pointers and references, let me elaborate on the differences between them from a compilation perspective:

The program adds pointers and references to the symbol table at compile time, where the variable name and the corresponding address of the variable are recorded on the symbol table. The corresponding address value of the pointer variable on the symbol table is the address value of the pointer variable, while the corresponding address value on the symbol table refers to the address value of the referenced object. The symbol table is not changed after it is built, so the pointer can change the object it points to (the value in the pointer variable can be changed), and the referenced object cannot be modified.

Finally, summarize the similarities and differences between pointers and references:

★ The same point:

Are the concept of the address;

The pointer points to a piece of memory whose contents are the address of the memory, and the reference is an alias to a block of memory.

★ Different points:

The pointer is an entity and the reference is only an individual name;

References can only be initialized once at definition, then immutable, pointer variable, reference "one-woman", and pointers may be "inconstant";

The reference has no const, the pointer has a const,const pointer immutable;

The reference cannot be null, the pointer can be empty;

The "sizeof reference" gets the size of the variable (object) to which it is pointed, and the "sizeof pointer" gets the size of the pointer itself;

The value of the pointer and the reference of the self increment (+ +) operation is different;

References are type-safe, and pointers are not (references are more than pointer type checking

Related Article

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.