Const and volatile analysis in C language, constvolatile
1. const read-only variable
The variable modified by const is read-only, essentially or variable
The local variable modified by const allocates space on the stack.
The global variables modified by const allocate space in the global data zone.
Const is only useful during compilation and useless during runtime
The variable modified by const is not a real constant. It only tells the compiler that the variable cannot appear on the left side of the assign value.
2. const global variable differences
In modern C language compilers, modifying the const global variables will cause program crashes.
Note:
The standard C language compiler does not store the global variables modified by const In the read-only storage area, but stored in the modifiable global data area. The value can still be changed.
Related programming experiments
#include <stdio.h>
Const int g_cc = 2;
Int main()
{
Const int cc = 1;
Int* p = (int*)&cc;
Printf("cc = %d\n", cc);
*p = 3; //Executively, it means that the variable decorated by const is not a constant in the true sense.
Printf("cc = %d\n", cc);
p = (int*)&g_cc;
Printf("g_cc = %d\n", g_cc);
*p = 4; //The compiler is VS2010, trying to modify the const global variable (stored in the read-only memory area), causing the program to crash. (Modern compilers cause programs to crash, while early compilers don't.)
Printf("g_cc = %d\n", g_cc);
Return 0;
}
Lab results
3. const nature
Const in C language makes the variable read-only and soothing
Const in the modern C compiler stores variables with a global life cycle in the read-only storage Zone
Const cannot define constants in the true sense.
Related lab code
#include <stdio.h>
Const int g_array[5] = {0}; // global scope const variable, stored in read-only storage area, cannot be modified
Void modify(int* p, int v) //Modify the value at the address pointed to by pointer p with V
{
*p = v;
}
Int main()
{
Int const i = 0;
Const static int j = 0; //Because of static modification, j life cycle is the life cycle of the program, so use const to modify the variable with global life cycle stored in read-only storage area, not modifiable
Int const array[5] = {0};
Modify((int*)&i, 1); // ok
Modify((int*)&j, 2); // error, can't modify the contents of the read-only memory area (the characteristics of modern compilers, the old compiler is not necessarily)
Modify((int*)&array[0], 3); // ok
Modify((int*)&g_array[0], 4); // error, can't modify the contents of the read-only memory area (the characteristics of modern compilers, the old compiler is not necessarily)
Printf("i = %d\n", i);
Printf("j = %d\n", j);
Printf("array[0] = %d\n", array[0]);
Printf("g_array[0] = %d\n", g_array[0]);
Return 0;
}
Lab results
4. const modifier function parameters and return values
The const modifier function parameter indicates that you do not want to change the parameter value in the function body.
The Return Value of the const modifier function indicates that the return value cannot be changed and is mostly used to return pointers.
TIPS:
Strings in the C language are stored in the read-only storage area, and the const char * pointer must be used in the program.
Related lab code
#include <stdio.h>
Const char* f(const int i)
{
i = 5; //error, const variable cannot be used as lvalue
Return "Hello World"; / / return a pointer to the read-only memory area
}
Int main()
{
Char* pc = f(0); //waring
Printf("%s\n", pc);
Pc[6] = '_'; //error, cannot modify the contents of the read-only memory area
Printf("%s\n", pc);
Return 0;
}
Lab results
5. Hidden volatile
Volatile can be understood as "compiler warning indicator"
Volatile tells the compiler that the variable value must be retrieved from the memory each time.
Volatile mainly modifies variables that may be accessed by multiple threads.
Volatile can also modify variables that may be changed by unknown factors.
Const Summary
Note:
Do not forget to dig for water.