C Language Leak Check--const

Source: Internet
Author: User

I. The role of the const
The const-modified variable becomes a constant that cannot be modified, such as
int c = 0;At this time, C can be modified, but with the const, the value of C becomes a constant,
const int c = 0;If this time makes c = 2;Then the program will get an error.
This is the main function of const, but, in C language, is a const-modified variable really a constant?

Second, the "bug" of the const
Let's look at the code below

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;}
    这段代码,运行结果如下


It's not a good deal. is a const-modified variable a constant? The constant is the amount of value that will not change after initialization until the end of the program. Why has it changed here?

Iii. several cases of const
Before answering the above questions, let's consider some of the things that const
1. const modifier static local variable
2. CONST modifier global variable
3. Const modifier local variable

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

Therefore, it is concluded that the const-modified variables in C ====> are not exactly constants.

Here 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;}

Iv. extension of the const
We can use this property of const to standardize some code writing.
1, the const modifier of the function parameters, inside the function is not allowed to modify.
2, const modified function return value, indicating that the return value of the function can not 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;  }  

C language Leak check--const

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.