Understand the difference between references and pointers in C++ in principle

C++ introduces the data type of reference, which can directly modify the data pointed to by the reference, which is similar to an alias like the operation of the original data; but the pointer operation needs to consider the reference and dereference, so what are the two What is the difference? Just from the grammar, I always forget it by rote, and it is not easy to use. The best choice is to understand from the principle. Next, I try to analyze it from the perspective of my personal understanding (may be wrong), and finally do Grammatical comparison.

References are just aliases and do not occupy memory

The purpose of designing references is only to free the programmer from pointer operations, such as int &i = a; For the compiler, operating the reference i and directly operating the original number a is an effect, the compiler does not distinguish between i and a, the compiler When compiling, i and a are directly represented by the same memory, and they occupy the same register during operation. After the compiler compiles, both i and a are converted to the compiler-recognized token x. It's like you have several nicknames for friends, but they all refer to you, and the household registration office does not distinguish between these, only the name registered in your household registration book is known.

A pointer is data and has its own memory

A pointer is a separate data structure that has its own memory and has nothing to do with the data it points to; it's just that the data of the pointer can manipulate the object it points to and modify the data pointed to by the object, which has nothing to do with the pointer itself. the difference.

In order to verify the above guess, test it with a small program, the code is as follows

#include<iostream>
using namespace std;

int main()
{
        int a = 1;
        int &i = a;
        int *p = &a; 

        cout << "&a = " << &a << endl;
        cout << "&i = "  << &i << endl;
        cout << " p = "  << &p << endl;

        return 0;
}

The results are as follows:

&a = 0x7ffc74318914
&i = 0x7ffc74318914
 p = 0x7ffc74318918

From the running result of the above program, we can see that the reference i and the original data a occupy the same memory, while the pointer has its own memory. (The memory allocation is so similar mainly because it is temporarily allocated through the stack. If the address of these variables is not forcibly accessed in the program, the compiler will not allocate memory for these data, and all are stored in registers)

Understanding references as macro definitions ( wrong!!! )

At the beginning, I always understood the reference as a macro definition. After the preprocessing, the references were all replaced with the pointed object, so the reference does not occupy a separate memory, but is just an alias of the object it refers to; the reference is understood as a macro definition It fully conforms to all the properties of references, but in fact references are not defined by macros. Let's preprocess the previous program and observe the preprocessed program

perform preprocessing

g++ -E reference.cc > reference.i

The main part after preprocessing (the irrelevant part is omitted):

using namespace std;

int main()
{
 int a = 1;
 int &i = a;
 int *p = &a;

 cout << "&a = " << &a << endl;
 cout << "&i = " << &i << endl;
 cout << " p = " << &p << endl;

 return 0;
}

We can see that the reference i is not interpreted as a macro, so the reference is not implemented by the principle of macro definition.

Compilation point of view

When the program is compiled, the pointer and the reference are added to the symbol table, and the variable name and the address corresponding to the variable are recorded in the symbol table. The address value corresponding to the pointer variable in the symbol table is the address value of the pointer variable, and the address value corresponding to the reference in 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 it points to (the value in the pointer variable can be changed), but the reference object cannot be modified.

grammatical difference

After understanding the principle, the grammatical difference is logical.

  • A pointer is an entity, and a reference is just an alias;
  • A reference can only be initialized once at the time of definition and is immutable after that; pointers are mutable;
  • References have no const, pointers have const, and const pointers are immutable;
  • A reference cannot be null, a pointer can be null;
  • "sizeof reference" gets the size of the pointed variable (object), and "sizeof pointer" gets the size of the pointer itself;
  • The self-increment (++) operations of pointers and references have different meanings;
  • References are type-safe, while pointers are not (references are more type-checked than pointers);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325920699&siteId=291194637