Pointer and reference

Source: Internet
Author: User

1. From a symptom perspective: A pointer can change the value it points to at runtime, and the reference will not change once it is bound to an object.

2. From the memory allocation perspective:ProgramAllocate memory areas for pointer variables, but do not allocate memory areas for references

3. In terms of compilation, the program adds pointers and references to the symbol table during compilation. the symbol table records the variable name and the address corresponding to the variable. The address value corresponding to the pointer variable in the symbol table is the address value of the pointer variable, and the address value referenced in the symbol table is the address value of the referenced object. The symbol table will not be changed after it is generated, so the pointer can change the object to which it points (the value in the pointer variable can be changed), but the referenced object cannot be changed.

Pointers and references look completely different (pointers use the operators "*" and "->" and reference the operators "."), but they seem to have the same functionality. Both pointer and Reference allow you to indirectly reference other objects. How do you decide when to use pointers and when to use references?

First, you must realize that reference pointing to null values cannot be used under any circumstances. A reference must always point to some objects. Therefore, if you use a variable and direct it to an object, but the variable may not point to any object at some time, you should declare the variable as a pointer, in this way, you can assign a null value to the variable. On the contrary, if the variable must point to an object, for example, your design does not allow the variable to be empty, then you can declare the variable as a reference. Because the reference will certainly point to an object, in C ++, the reference should be initialized. The fact that there is no reference pointing to a null value means that the referencedCodeEfficiency is higher than pointer usage. Because you do not need to test its validity before using the reference. Instead, pointers should always be tested to prevent null pointers from being referenced. Another important difference is that pointers can be re-assigned to point to another object. However, the reference always points to the specified object during initialization and cannot be changed later. In general, you should use pointers in the following situations. First, you should consider the possibility of not pointing to any object (in this case, you can set the pointer to null ), second, you need to be able to point to different objects at different times (in this case, you can change the pointer ). If you always point to an object and it does not change its direction once it points to an object, you should use references.

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.