Same point:
1. Pointers and references are the concepts of addresses. The pointer points to a piece of memory, the content of which is the address of the memory pointed to, and the reference is the alias of a block of memory.
2. References are implemented within the language using pointers.
3. The reference is generally understood as a pointer and does not make a serious semantic error. A reference operation can be seen as a restricted pointer (only allowed to take content operations).
Different points:
1. The pointer is logically independent, can change the contents of the pointer, it can also change the contents of the memory pointed to by the pointer, but the reference is only an alias, logically independent, its existence is dependent, must be initialized at the time of declaration, and the referenced object can not be changed throughout the life cycle, But the contents of the memory it points to can be changed.
2. The difference in the statement: the declaration of a pointer, such as int* p, can be declared independently, whereas a reference declaration, int A; int &b = A; must be attached to a variable. The pointer stores an address that points to a storage unit of memory, whereas a reference to the original variable is essentially a thing, but an alias of the original variable.
3. The pointer needs to be dereferenced, that is, to add the * symbol, while the reference is used without a reference, can be used directly.
4. The pointer can be modified with a const modifier, the contents of the constant pointer (that is, the stored address) is immutable, and the reference cannot be modified with const because it cannot be changed by itself.
5. "sizeof (Reference)" Gets the size of the variable (object) pointed to, and "sizeof (pointer)" Gets the size of the pointer itself data type.
6. Pointers are different from the reference's increment (+ +) operation, where the pointer is the self-increment of its contents (that is, the stored address), whereas the reference is its content that points to memory.
7. The pointer can be changed after initialization, whereas a reference may only be initialized at the time of declaration and cannot be changed.
8. The value of the pointer can be null, that is, it does not point to any memory address, and the reference must be initialized at the time of definition and cannot be null.
9. Pointers can have multiple levels, whereas references can only be one level.
10. The reference is type-safe, and the pointer is not, and the reference is more type-checked than the pointer.
11. As a function parameter, the pointer is actually a value pass, the address of the pointer is copied, assigned to the function parameter, the function parameter in the stack opens the memory space to hold the address value passed by the argument, thus forming a copy of the argument, The value passed to the feature is that any operation of the called function on the formal parameter is done as a local variable without affecting the value of the argument variable of the main function. In the process of function passing, the parameters of the called function are also used as local variables to open up the memory space in the stack, but this is the address of the argument variable that is put in by the main function, there is no copy operation, and the operation of the called function to the formal parameter is handled as an indirect address. This means that the argument variables in the keynote function are accessed through the addresses stored in the stack, so any operation of the called function on the formal parameter affects the argument variable of the keynote function.
12. The program adds pointers and references to the symbol table at compile time, and the name of the variable and the 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, 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 modified.
13. Pointer passing parameters are duplicated on the stack, and direct use of pointers can easily cause address overflow errors, while references reduce the duplication of large amounts of data over the stack and avoid the occurrence of address overflow errors.
The difference between a pointer and a reference