1、C中的const

  • Local const variables are stored on the stack and allocated memory (that is, they can be changed indirectly by address). The test code is as follows:
void main(a)
{
 const int a = 10;
 printf("initial a = %d\n", a);
 int *ptr = &a;
 *ptr = 100;
 printf("modify a = %d\n", a);
}
Copy the code

Running results:

  • 2, the global const variable is stored in a read-only data segment (can’t be changed by address, can’t be written to error), and can be used in other source files by default.
const int a = 10;
// We cannot initialize global const variables with variables, the compiler does not pass: expressions must contain constant values
//const int b = a; 
void main(a)
{
	printf("first a = %d\n", a);
	int *ptr = &a;
	// The compiler passed, but a write error occurred at runtime
	*ptr = 100;
	printf("second a = %d\n", a);
}
Copy the code

Running results:

2. Const in C++

  • 1. Plain local const variables

For basic types, initialized with data in the symbol table of common local const variable, at this time will not allocate memory, put it in the symbol table, if it addresses, will open up a new space, will create a temporary variable that is to say, if by indirect address modified value does not affect the itself. The test code is as follows:

	const int a = 10;
	int *ptr = const_cast<int*>(&a);
	*ptr = 100;
	cout <<"a = "<<a << endl;
	cout <<"*ptr = "<<*ptr << endl;
Copy the code

Running results:

int temp=a; int *ptr=&temp; // So the operation on the memory space pointed to by the pointer PTR does not affect a.Copy the code

For primitive types, ordinary local const variables initialized with a variable open up memory in the stack area and can be changed indirectly by address. The test code is as follows:

	int b = 10;
	const int a = b;
	int *ptr = const_cast<int*>(&a);
	*ptr = 100;
	cout << "a = " << a << endl;
	cout << "b = " << a << endl;
	cout << "*ptr = " << *ptr << endl;
Copy the code

Running results:

(3) For custom types, memory is allocated, and values can be changed indirectly through the address

class Person
{
public:
	Person(int age) { this->age = age; };
	~Person() {};
	int age;
private:
};
int main()
{
	const Person personA(20);
	Person* personPtr = const_cast<Person *>(&personA);
	personPtr->age = 100;
	cout <<"personPtr->age = "<<personPtr->age << endl;
}
Copy the code

Running results:

  • 2. Plain global const variables

    • Much the same as local const variables, except that memory is stored in the global/heap area. For c++, global const variables are internally bound by default and can be declared externally when defined with the extern keyword. Because the sub-document preparation is not good to show, please try by yourself.
  • 3, const member variable/member function

    • Const member variables can only be accessed by const member functions

    • A const member function can access all member variables, but cannot directly modify the values of ordinary member variables in the function body. To modify the values of ordinary member variables in the function body, you need to add the mutable keyword in front of the variable definition, or modify them indirectly through the address. Note: Const member functions can only be accessed by const objects of that class. The test code is as follows:

class Person
{
public:
	Person() {};
	~Person() {};
	void testConst {// const member functions that are not mutable cannot be modified directly //b = 20; Int * Bptr = const_cast<int*>(&b); *Bptr = 1000; // mutable variables can be modified by c = 30; cout << b << endl; cout << c << endl; } private: const int a=10; int b = 100; mutable int c = 20; }; intmain()
{
	Person p;
	p.test();
	return 0;
}
Copy the code

Running results:

C/C++ const

  • 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.

  • 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.

  • Local const variables are treated differently in c++ :

    • (1) For the underlying data type, const int a = 10, the compiler places it in the symbol table and does not allocate memory. When it fetches an address, it allocates memory. A is in the symbol table, and when we address a, we allocate new space for A, and *p operates on the allocated space, and A is the value we get from the symbol table.

    • (2) For the underlying data type, if we initialize a const variable with a variable, if a const int a = b, we will allocate memory to a.

    • (3) Memory is also allocated for custom data types, such as class objects.

  • (c) const is external by default, and c++ const is internal by default. 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. Extern const int A = 10; extern const int a = 10; extern const int a = 10;