C language leak detection - const

1. The role of
const A const-modified variable becomes an unmodifiable constant. For example, at
int c = 0;this time, c can be modified, but after adding const, the value of c becomes a
const int c = 0;constant c = 2;. will report an error.
This is the main role of const, but, in C language, is a variable modified by const really a constant?

Second, the "bug" of const,
let's look at the following code

int main()
{
    const int cim_test = 1;

    printf("cim_test = %d\n", cim_test);

    int *p1 = &cim_test;

    *p1 = 3;
    printf("cim_test = %d\n", cim_test);

    return 0;
}
    这段代码,运行结果如下

C language leak detection - const
Isn't it said that a good const-modified variable is a constant? What is a constant, after initialization, until the end of the program, the value will not change. Why has it been changed here?

3. Several situations of const
Before answering the above questions, let's consider several situations in which const will appear.
1. const modifies static local variables
2, const modifies global variables
3, const modifies local variables

In the first case, the local variables at this time are more affected by static, so they are stored in the read-only storage area and cannot be modified.
In the second case, it depends on the compiler at this time. If it is an early compiler, such as BCC, the variables are stored in the global data area, and if there are some modern C compilers first, then the compiler will be optimized to store it in read-only storage.
The third case, which is more common, is that the variable is stored on the stack, so we can point to this memory through a pointer, and then modify the value .

Therefore, it is concluded that ====> In the C language, const-modified variables are not completely constants.

Below is the test code

#include <stdio.h>

const int cig_test = 2;

int main()
{
    const int cim_test = 1;
    const static int cism_test = 3;

    printf("cim_test = %d\n", cim_test);

    int *p1 = &cim_test;

    *p1 = 3;
    printf("cim_test = %d\n", cim_test);

    int *p2 = &cig_test;
    *p2 = 4;       //不确定,需要知道编译器是现代编译器,还是古老的标志编译器
    printf("cig_test = %d\n", cig_test);

    int *p3 = &cism_test;
    *p3 = 5;      //报错,加上static修饰的变量,存储在只读存储区
    printf("cism_test = %d\n", cism_test);

    return 0;
}

Fourth, the extension
of const We can use this property of const to standardize some code writing.
1. The const-modified function parameters are not allowed to be modified inside the function.
2. The return value of a const-modified function means that the return value of the function cannot be modified
#include <stdio.h>

const char* f(const int i)  
{  
    i = 5;    //error,const变量不能作为左值  

    return "Hello World";    //返回指向只读存储区的指针  
}  

int main()  
{  
    char* pc = f(0); //waring  

    printf("%s\n", pc);  

    pc[6] = '_';    //error,不能修改只读存储区的内容  

    printf("%s\n", pc);  

    return 0;  
}  

Guess you like

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