Const literally means constant, constant. It is a key word in C/C ++. It is a qualifier that specifies that a variable cannot be changed. It converts an object to a constant.

Similarities and differences between a:

C globals are stored in read-only data segments. C++ global const when declaring extern or addressing a variable, the compiler assigns storage addresses that are stored in read-only data segments. Both are protected by read-only data segments and cannot be modified.

      const int constA = 10;
  
      int main(){
  
           int* p =  (int*)&constA;
  
           *p = 200;
  
     }
Copy the code

The above code was compiled in C/C ++. At runtime, a write error occurred while modifying constA. The reason is to modify the data in the read-only data segment.

Similarities and differences between two:

In C, local const values are stored on the stack. However, we cannot change const read-only variables directly through variables. We can change const values indirectly through Pointers, bypasses the compiler.

      const int constA = 10;

      int* p = (int*)&constA;

      *p = 300;

      printf("constA:%d\n",constA);

      printf("*p:%d\n", *p);
Copy the code

Running results:

    constA:300
    *p:300
Copy the code

C modifies constA via indirect pointer assignment. Local const variables are treated differently in c++ :

2.1 For the underlying data type, const int a = 10, the compiler places it in the symbol table and does not allocate memory.

   const int constA = 10;
 
   int* p = (int*)&constA;
 
   *p = 300;
 
   cout <<  "constA:" << constA << endl;
 
   cout << "*p:"  << *p << endl;
Copy the code

Running results:

   constA:10
   *p:300
Copy the code

ConstA In the symbol table, when we address a constA, we’re assigning a new space to a constA. *p is the allocated space, and constA is the value we get from the symbol table.

2.2 For the underlying data type, if we initialize a const variable with a variable, if a const int a = b, then a is allocated memory.

  int b = 10;

  const int constA = b;

  int* p = (int*)&constA;

  *p = 300;

  cout <<  "constA:" << constA << endl;

  cout << "*p:"  << *p << endl;
Copy the code

Running results:

   constA:300
   *p:300
Copy the code

ConstA allocates memory, so we can change the value in constA memory.

2.3 Memory is also allocated for custom data types, such as class objects.

const Person person; //person. Age = 50; Person* pPerson = (Person*)& Person; PPerson ->age = 100; cout <<"pPerson->age:"  << pPerson->age << endl;

  pPerson->age = 200;

  cout <<  "pPerson->age:" << pPerson->age << endl;
Copy the code

Running results:

   pPerson->age:100
   pPerson->age:200
Copy the code

Person is allocated memory, so we can modify the Person object through indirect assignment of Pointers.

Similarities and differences between the three:

Const defaults to external in C and internal in C++. If C files contain const int a, the compiler will report a redefinition error. In C++, it does not, because const in C++ is wired internally by default. If we want const in C++ to have an external connection, we must display the declaration as:

   extern const int a = 10;
Copy the code