[Pointers, references, and array names in js-c++]c++

Source: Internet
Author: User

Pointers and references learn for a long time, or occasionally confused, simple record analysis of the difference it, remember that the new difference is constantly updated to add in

1. Pointers can be reassigned multiple times, references cannot be reassigned, and must be initialized at the time of declaration

2, the pointer can point to NULL (that is defined as nullptr), but the reference must point to a thing

3, there is no "reference calculation" this thing (but you use &d+5 this does not error, also can calculate things)

4. A pointer has its own memory address and size on the stack (4 bytes on x86), while references share the same memory address (with the original variable), but also occupy some space on the stack. Because the reference has the same address as the original variable itself, it is safe to treat the reference as another name for the same variable. Note: The pointer to the content can be on the stack or heap. Ibid. reference. My assertion in this statement is not that the pointer must point to the stack. The pointer is simply a variable that holds the memory address. This variable is in the stack. Because the reference has its own space on the stack, and the address is the same as the variable it references. More about stacks vs heaps. This means that there is a compiler that does not tell you the real address of the reference.

One of our default rules:

· Use references in function parameters and return types to provide useful and self-documenting interfaces.

· Use pointers to implement algorithms and data structures.

About array names and pointers:

The array name in the expression is treated as a pointer to the first element of the array by the compiler.

The subscript is always the same as the pointer offset.

In the declaration of a function argument, the array name is treated as a pointer to the first element of the array (in other words, the array name is degraded to a pointer when the function is called, but the sizeof function does not take the array size).

For the array name, pointers, and the addition of references:

Both the array name and the pointer can do arithmetic operations (but the multiplication method should be meaningless), but the array name does not have to be assigned a value back, and the pointer can (in other words, the name-increment-decrement operator cannot be used for an array)

Suppose there is an array a[10]

1 /*   2 A:          0012ff20  3&a:         0012ff20  4a+1:        0012ff24   5 &a+1:       0012ff48  6* /  

A and &a point to the same piece of address, but their effect after +1 is different, a+1 is the memory size of an element (increase 4), and &a+1 increases the memory size of the entire array (40 increase)

&a is the address of the entire array! Both the array name fetch address is equivalent to the logarithmic group fetch address. (instead of the imaginary, the array name is a pointer, and the pointer is the pointer to the pointer)

I don't see a place to use &a like this yet.

[Pointers, references, and array names in js-c++]c++

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.