The difference between C + + pointers and references in the "C + +" of "College Recruit Interview" question 13th

Source: Internet
Author: User


1, the definition of pointers and references and the nature of the difference:

(1) Pointer: The pointer is a variable, except that the variable stores an address, pointing to a memory cell, whereas a reference is essentially the same thing as the original variable, but an alias of the original variable. Such as:

int A=1;int *p=&a;

int A=1;int &b=a;

The above defines an shaping variable and a pointer variable p, which points to the storage unit of a, that is, the value of P is the address of a storage unit.

and the following 2 sentences define an shaping variable A and a reference B to shape A, in fact A and B are the same thing, in memory occupy the same storage unit.

(2) can have a const pointer, but no const reference;

(3) Pointers can have multiple levels, but the reference can only be one level (int **p; legal and int &&a is illegal)

(4) The value of the pointer can be null, but the reference value cannot be null, and the reference must be initialized at the time of definition ;

(5) The value of the pointer can be changed after initialization, that is, pointing to other storage units, and the reference will not change after initialization .

(6) "sizeof reference" gets the size of the variable (object) that is pointed to, and the "sizeof pointer" gets the size of the pointer itself;

(7) The pointer and the reference of the self-increment (+ +) operation meaning is different;

2. The difference between a pointer and a reference when passed as a function parameter:

(1) The reference and the pointer look the same, but in essence there is a difference: The pointer passing parameter is essentially the way the value is passed , and it passes an address value. The characteristic of value passing is that any operation of the function on the formal parameter is done as a local variable, without affecting the value of the argument variable of the main key function.

In the process of value passing, the parameter of the function is called as local variable of the function, that is, the memory space is opened in the stack to hold the value of the argument that is put in by the main function, and the value passing is a one-way pass (argument-and-parameter), and the actual parameter has no relation with the formal parameter. How does the pointer pass through this local variable to access the argument, of course, is through the local variables stored in the address.
Since the parameters and arguments are independent of each other, when there are no modifiers, the parameters can be modified, the parameter pointers can point anywhere, and the arguments cannot be accessed again after the modification.

(2) While in the reference transfer process, the formal parameters of the called function are also used as local variables to open up the memory space in the stack, but the address of the argument variable that is put in by the key function is stored. Any operation of the modulated function on the formal parameter is handled as an indirection, that is, accessing the real parametric in the central melody function through the address stored in the stack . Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

Reference passing and pointer passing are different, although they are a local variable on the function stack space, but any handling of reference parameters is handled by an indirection to the relevant variables in the key function. for pointers passing parameters, if you change the address of the pointer in the called function, it will not affect the relevant variable of the key function .

both use the address to manipulate the value, but if you change the value (address) of the pointer, the real arguments is not affected (the parameter is a local variable). After compiling, pointers and references are no different .

The difference between C + + pointers and references in the "C + +" of "College Recruit Interview" question 13th

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.