The connection and difference between reference and pointer in C / C ++

The connection and difference between reference and pointer in C / C ++

Why should pointers be used in C / C ++?

1. Every programming language uses pointers. C ++ exposes pointers to users (programmers), while languages ​​such as JAVA and C # hide pointers.
2. The pointer can effectively represent the data structure;
3. Can dynamically allocate memory to realize the free management of the memory
4. Convenient to use the string and efficient use of the array
5. The pointer is directly related to the storage address of the data, such as: value transfer is not as good as address transfer Efficient, because the value transfer first takes the value from the address of the actual parameter, and then assigns the value to the formal parameter into the function calculation; and the pointer directly points the address of the formal parameter to the actual parameter address, and directly fetches the data when used, the efficiency is improved, especially in frequent assignment In other cases (note: the change of the formal parameters will affect the actual parameters!)

The difference between reference and pointer?

First with the plain words of introduction:
pointer - for a type T, T is a pointer type T, that is a T -type variable can hold the address of an object T, and T is the type you can add some qualifiers, Such as const, volatile, etc. See the figure below, the meaning of the pointer shown:
Insert picture description here
Reference -Reference is an alias of an object, mainly used for function parameters and return value types, the symbol X & indicates a reference of type X. See the figure below, the meaning of the quotes shown:
Insert picture description here

  1. The pointer can modify the value pointed to by the address, it can also point to other addresses; and the reference always points to the object specified at initialization , but can modify the value of the object
  2. From the point of view of memory allocation, the program allocates the memory area for the pointer variable, but not for the reference , because the reference must be initialized when it is declared to point to an existing object. References cannot point to null values.
  3. From the perspective of compilation, the program adds pointers and references to the symbol table during compilation, and the symbol table records the variable name and the address corresponding to the variable. 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 of the reference on the symbol table is the address value of the reference object . After the symbol table is generated, it will not be changed, so the pointer can change the object pointed to (the value in the pointer variable can be changed), and the referenced object cannot be changed. This is the main reason why using pointers is unsafe and using reference security. In a sense, a reference can be considered a pointer that cannot change the address.
  4. The fact that there are no references to null values ​​means that the code using references is more efficient than using pointers. Because you don't need to test the validity of the reference before using it. Instead, pointers should always be tested to prevent them from being empty.
  5. In theory, there is no limit to the number of pointer levels, but the reference can only be one level . As follows:
    int ** p1; // legal. Pointer to pointer
    int * & p2; // legal. Reference to pointer
    int & * p3; // Illegal. The pointer to the reference is illegal
    int && p4; // illegal. References to references are illegal.
    Note that the above reading is from left to right .

In short, it can be concluded that "a pointer points to a piece of memory, and its content is the address of the pointed-to memory; and a reference is an alias of a piece of memory, and the reference does not change the pointing ."

Special const

int main()
{
    int i= 0;
    int j=1;
    //常量引用,指向的地址的内容是个常量,不可以修改,即i的值不可以修改
    const int &r = i  ;
    
    //常量指针,p指针的地址可修改,但无法修改指向地址的内容,*p不可修改,即p指针不可以修改j的值,但可以指向i的地址
    const int* p=&j;
    p = &i;

    //指针常量,q指向的地址不可修改,可以修改指向地址的内容,*q可以修改,即q指针只能指向j的地址,但可以修改j的值
    int* const q = &j;
    *q=5;
    
    //常量指针常量,w指向的地址和内容都不可修改,即w和*w均不可修改
    const int* const w = &j;
    
    cout<<j<<endl;
    cout<<i<<endl;
    return 0;
}

Pointer passing and reference passing

  1. The pointer transfer parameter is essentially a way of value transfer, what he passes is an address value. In the process of value transfer, the formal parameters of the called function are treated as local variables of the called function, that is, the memory space is opened on the stack to store the value of the actual parameter put in by the main calling function, thus becoming a copy of the actual parameter . The characteristic of value transfer is that any operation of the called function on the formal parameters is carried out as a local variable , which does not affect the value of the actual parameter variable of the calling function.
  2. In the process of reference passing, the formal parameters of the called function also open up memory space on the stack as local variables, but at this time the address of the actual parameter variable put in by the calling function is stored. Any operation of the called function on the formal parameter is processed into indirect addressing , that is , the actual parameter variable in the calling function is accessed through the address stored in the stack . Because of this, any operation of the called function on the formal parameters affects the actual parameter variables in the calling function .

Reference passing and pointer passing are different. Although they are all local variables on the stack space of the called function, any processing of reference parameters will operate on the related variables in the calling function through an indirect addressing method. For the parameters passed by the pointer, if the pointer address in the called function is changed, it will not affect the related variables of the calling function. If you want to change the relevant variables in the calling function through the passing of pointer parameters, you must use a pointer to a pointer , or a pointer reference .

to sum up

Finally, summarize the similarities and differences between pointers and references:
★ Similarities:

  1. All are concepts of addresses; a
    pointer points to a piece of memory, and its content is the address of the pointed-to memory; and a reference is an alias of a piece of memory.
    ★ Differences:
  2. The pointer is an entity, and the reference is only an individual name;
  3. References can only be initialized once at the time of definition, and then immutable; pointers are variable; references "from one to the other", pointers can "see the way"
  4. References do not have const, pointers have const, and const pointers are immutable;
    specifically, there is no such form as int & const a, and const int & a does exist. In this form, the value of the latter guideline cannot be changed)
  5. The reference cannot be null, the pointer can be null;
  6. "Sizeof reference" is the size of the variable (object) pointed to, and "sizeof pointer" is the size of the pointer itself 4
  7. The meaning of the pointer and reference increment (++) operation is different;
  8. References are type safe, and pointers are not (references have more type checks than pointers)
Published 13 original articles · praised 5 · visits 459

Guess you like

Origin blog.csdn.net/why18767183086/article/details/104069687