Const description in C ++

Source: Internet
Author: User

I recently read the programmer's interview book and found a lot of questions about const. I checked a lot of information and can summarize it as follows:

1. in C Language

  In C language, the variable modified by const does not have the constant feature, but is an unchangeable variable. In essence, it is still a variable and cannot be known during compilation, it cannot be used as an array subscript.

2. In C ++

In C ++, const is quite different. In C ++, const is encouraged to replace # define. In C ++, the variables defined by const are divided into two types:

Case 1 (allocate space in the. rodata segment ):

If the const is used globally or the static keyword is used, for example, extern const int I = 10, static const int I = 10. Then this I is a constant (a constant in the real sense is said on the Internet), and the constant is stored in. the rodata segment cannot be modified by taking the address (for details, see Case 2). If you modify the segment, a segment error is reported.

Case 2 (do not allocate space in the. rodata segment ):

If the const is used locally without the static keyword, for example, in the main function, const int I = 10*2 + 1, in this case, if the constant: 1) A value assignment is a constant expression (there are no other variables or values that require external input). 2) if the constant is not subjected to an operation similar to an address (&), no space will be allocated to I, only put it in the symbol table; otherwise, I will be allocated space, and this space is on the stack, different from the global space. on the rodata segment. In addition, note that there may be some illusion when the compiler is used for disassembly. For example, in the DEBUG mode, the compiler generates assembly code for better debugging, so in any case, the space will be allocated to the const constant, but If you enable the compiler's O2 optimization option, no space will be allocated.

However, no matter no space is allocated, the compiler will make an optimization as long as the constant value is a constant expression (there are no other variables or values that require external input, it is called const folding. In short, it means that during compilation, I will be replaced directly with 21 wherever I is used.

A. constant folding

If I is a constant expression (no other variables or a value that needs to be input from outside), that is, the I value does not need to be determined by the bucket, in this case, any place where I occurs in the program has been replaced during compilation. Even if you change this value by taking the address, it is equivalent to changing a copy, as shown below:

Const int I = 1;

Int * p = (int *) & I;

* P = 2;

Cout <* p <endl <I <endl;

Output result:

2

1

This indicates that if the value of I is determined, any place where I appears in the program will be replaced by 1.

B. Constant-free folding

In another case, if the value assignment is not a constant expression, you need to access the storage area to obtain the exact value. In this case, there is no "constant folding", as shown below:

Int I = 10;

Const char gc = cin. get (); // or const char gc = I are the same

Char * t = (char *) & gc;

* T + = 2;

Cout <* t <endl <gc <endl;

Input:

Output:

C

C

From the above situation, we can see that although gc is a local const, its value is uncertain and needs to be input by the user, so it will allocate space for gc in the stack, and because the gc value is uncertain, it cannot be optimized by constant folding. If gc is used in the program, you must directly access the stored area to obtain the value. Therefore, the * t and gc values are consistent.

Without constant folding, memory space must be allocated, because the essence of non-constant folding is that the value is uncertain and needs to be obtained in the memory. In the case of constant folding, the memory space may be allocated or not allocated. If it is allocated, it is because the constant is subjected to the address fetch operation.

Volatile Keyword:

In addition, the volatile keyword can also block constant folding, as shown below:

Volatile const int I = 1;

Int * p = (int *) & I;

* P = 2;

Cout <* p <endl <I <endl;

Output result:

2

2

 

The above is the comprehensive information I obtained. Specifically, the usage of const can be divided into global and local (the static keyword is a factor ), local memory can be divided into allocation memory and non-allocation memory, constant folding and non-constant folding, the unallocated memory is determined by whether to perform address-related operations on the constant (such as & or directly taking the value from the memory). Whether the constant is folded depends on whether the value needs to be directly taken from the memory.

 

There are many shortcomings. Please point out that you can learn from each other and make progress. Don't try it. Thank you.

Reprinted, please describe the source

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.