How does the C ++ compiler allocate storage space for Const constants?

Source: Internet
Author: User

In the so-called C ++ compiler, the C ++ compiler is a highly compatible compiling environment in C ++. the compiler will perform different optimizations on different CPUs, the following describes how the C ++ compiler allocates buckets for Const constants.

Const is a type modifier commonly used in C ++. It has some subtle application scenarios. If you do not understand the source, errors are inevitable. This article will analyze const. Trace its source and investigate its essence, hoping to help you understand const. Based on the undertaking relationship of thinking, this article will be divided into the following parts. The C ++ creator introduced or retained the const keyword based on the purpose ?, This is an interesting and useful topic and is helpful for understanding const.

1. as we all know, C ++ has a strict type compilation system, which allows many errors of C ++ programs to be found in the compilation stage, greatly reducing the error rate, it has also become an outstanding advantage in comparison with C ++.

2. common Pre-processing commands in C ++ # define VariableName VariableValue can be easily used for value substitution. This value substitution has at least three advantages: first, it avoids the appearance of numbers with vague meanings and makes the program semantics clear and fluent.

Second, you can easily adjust and modify parameters. In the preceding example, when the number of people changes from 107 to 201, you can change the number here. Third, the program execution efficiency is improved, because the pre-compiler is used for value substitution, it does not need to allocate storage space for these constants, so the execution efficiency is high. In view of the above advantages, the use of such predefined commands is everywhere in the program.

3. Speaking of this, you may be confused about the relationship between the first and second points and const ?, Okay. Next, let's look at the following:

The pre-processing statement has many advantages, but it has a fatal disadvantage, that is, the pre-processing statement is only a replacement of simple values and lacks a type detection mechanism. In this way, the pre-processing statement cannot enjoy the advantage of strict C ++ type checks, which may cause a series of errors.

4. Now, the first stage has come to the conclusion:

Conclusion: The initial purpose of Const is to replace precompiled commands, eliminate its disadvantages, and inherit its advantages.

Now its form is changed:

Const DataType VariableName = VariableValue; why can const replace predefine statements? What is the magical power of const so that it can shake its arm to replace the predefined statement?

1. First, the constant value modified by const is non-mutable, which is the basis for replacing predefine statements.

2. Second, it is obvious that it can also avoid the appearance of numbers with vague meanings, and it is also convenient to adjust and modify parameters.

3. third, the C ++ compiler usually saves them in the symbol table instead of allocating storage space for common const constants, which makes them a constant during compilation, without the storage and read memory operations, the efficiency is also very high. At the same time, this is also an important basis for replacing predefine statements.

Here, I would like to mention why this is also the basis for it to replace the pre-defined statements, because the compiler will not read the stored content, if the compiler allocates storage space for const, it cannot be a constant during compilation.

4. Finally, the const definition is like a normal variable definition. It will be checked by the compiler for its type, eliminating the hidden danger of predefine statements.

Analysis: The above statement limits that the return value of a function cannot be updated. When the function returns an internal type such as fun1), it is already a numerical value. Of course, it cannot be updated by a value assignment. Therefore, const is meaningless at this time. It is best to remove it to avoid confusion. When the function returns a custom type such as fun2), this type still contains variable members that can be assigned values, so it makes sense at this time.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.