A common analysis of const constants usage in C + + explained

Source: Internet
Author: User
Tags modifier
First, the const in C and C + + are explained, the former const-modified variable is not a true constant, it just tells the compiler that the variable cannot appear on the left side of the assignment symbol. The latter C + + on the basis of C on the evolution of the const process.

1. In the C language const :

    • constThe modified variable is read-only, essentially or variable

    • constModified local variables allocate space on the stack

    • constModified global variables allocate space in read-only storage

    • constOnly useful at compile time, invalid at run time

    • constYou can't define constants in real sense.

constA modified variable is not a true constant, it simply tells the compiler that the variable cannot appear to the left of the assignment symbol. const Local variables are allocated space on the stack and can be changed by pointers to the values inside the space. After the compilation period, const the variable's constant property, the read-only feature is not, the read-only feature is only valid at compile time, the runtime is not valid at all. The const modified global variable allocates space in the read-only store, so if you use the pointer to modify the modified const global variable, the program crashes because the program reads the contents of the read-only store, and most of the programs crash.

2. In C + + const :

C + + On the basis of the evolution of the const process, the specific performance in:

    • constWhen declaring, put constants in the symbol table

    • Constants found in the compilation process are replaced directly by values in the symbol table (constant folding)

    • It is also possible to allocate storage space for the corresponding constants during compilation:

      • constUsed in the global or static keyword description, stored in the read-only data area

        extern const INT i = 10;static const int i = 10;//or modifier global variable const int a =10;int main () {}
      • Operators are used for constants in local variables to const & allocate space in the stack area

Note: Although the C + + compiler may const allocate space for a constant, it does not use the value in its storage space

A symbol table is a data structure generated during the compilation process.

#include <iostream> #include <string>using namespace Std;const int i = ten;          If I is changed by the pointer, a segment error occurs: Attempt to modify the read-only data area data int main () {    const int a = 5;    int *p = (int *) &a;    &a, the A identifier is allocated space, and P points to the space,                           //can access the address through *p, but not through a to access    *p = ten;               You cannot change the value of a by using the pointer    cout << a << Endl;    cout << *p << Endl;        return 0;}

3. Comparison with macro definition

Constants in C + + are const similar to macro definitions

const int c = 5;//Similar to # define C 5

However, the cosnt difference from a macro definition is:

    • constConstants are compiler-processed

    • Compiler cosnt type check and scope check for constants

    • The macro definition is handled by the preprocessor, only for simple text substitution

#include <stdio.h>void f () {    #define a 3    const int b = 4;} void G () {    printf ("a =%d\n", a);    In the G function to access the macro definition in the F function, there is no problem    //At the time of preprocessing the macro substitution, to the compiler, is printf ("a =%d\n", 3);    A macro is a constant that has no scope concept        //const definition, is handled by the compiler, is scoped, cannot access B    printf ("b =%d\n", b);} int main () {    const int A = 1;    const int B = 2;    int Array[a + B] = {0};        /* C Compile the const modifier is only a    variable with a read-only attribute, the size of the array is determined by the size of the two variables, the result of the    addition of two variables need to be run at the time until, so the compiler compile time do not know the length of the array, direct error    * /        * C + + compiler    Const is a definition of the true meaning of the constant, directly from the symbol table to take the value, compile time to know the value of a and B,    you can get the length of the array, will not error    */    int i = 0;        for (i=0; i< (A + B); i++)    {        printf ("array[%d] =%d\n", I, Array[i]);    }        f ();    g ();        return 0;}

Related articles:

Analysis of the difference between const and define in PHP

Related videos:

PHP Bottom Analysis Video tutorial

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.