A discussion on value passing, pointer passing and reference passing in C + + caused by *&

Source: Internet
Author: User

A discussion on value passing, pointer passing and reference passing in C + + caused by &*

The problem out of today in writing Leetcode, discuss found in the &* wording, so found some relevant information, and collation as follows.

First, say something in C + + A and&a the difference between a function as a parameter

In the previous write code, we encountered a shape like

void foo(int *ptr);

When such a function is declared, the function's formal parameter is a pointer, it is actually a value pass, if not clear please continue to look down , for example, we call

int3;int *pValue = &a;foo(pValue);

In this case, the value of pvalue is not changed by the Foo function, which means that pvalue is definitely pointing to a. For example, let's do the following in the Foo () function

void foo(int *ptr){    nullptr;}

After Foo (pValue) is called, PValue is still the address of a, *pvalue still 3.

If we change the statement

void foo(int*& ptr){    nullptr;}

After Foo (pValue) is called, PValue becomes a null pointer.

Why is it?

To start with value passing, pointer passing, and reference passing.

Value passing

A parameter is a copy of a pointer to an argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the called function, a value pass is one-way (argument-and-parameter), and the value of the argument can only be passed in and not outgoing. The value is passed when the parameter needs to be modified inside the function and you do not want the change to affect the caller.

Pointer passing

A pointer, in fact, is a copy of the address, which is actually a value pass . You can't change the value of the original variable, but you can change what the address points to.

Along the foo(int *ptr) way, you can *ptr = 5 change the value of the pvalue point (that is, a), but not the null pointer from within the function ptr=nullptr pValue .

Reference delivery

The parameter is equivalent to the "alias" of the argument, the operation of the formal parameter is actually the operation of the argument, in the reference pass process, the called function of the formal parameter although also as a local variable in the stack to open up memory space, but this is stored by the main function is put in the address of the argument variable. Any operation of the modulated function on the formal parameter is handled as an indirect addressing, that is, the argument variables in the central Melody function are accessed through the address stored in the stack.

To further deepen the distinction between pointers and references, the difference between them is illustrated from the compiler's perspective.

The program adds pointers and references to the symbol table at compile time, with the name of the variable and the address of the variable being 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, and the corresponding address value on the symbol table refers to the address value of the Reference object. The symbol table does not change after it is generated, so the pointer can change the object it points to (the value in the pointer variable can be changed), and the reference object cannot be changed.

Finally, summarize the same points and different points of pointers and references
    • Same point
      1. The pointer points to a piece of memory whose contents are the address of the referred memory, while the reference is the alias of a block of memory
    • Different points
      1. The pointer is an entity, and the reference is an alias
      2. A reference can be immutable only once the definition is initialized, and the pointer is mutable. Reference "mindedness", pointer can "Inconstant"
      3. The reference does not have a const, the pointer has a const, the concrete content see here
      4. Reference cannot be null, pointer can be null
      5. sizeof (reference) Gets the size of the object that is pointed to, and sizeof (the pointer) gets the size of the pointer itself
      6. Pointers and references have different meanings for the self-increment operation
      7. The reference is type-safe, and the pointer is not. A reference is a type of check that is more than a pointer.

A discussion on value passing, pointer passing and reference passing in C + + caused by *&

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.