[C ++ learning] Explain const keywords

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Let's take a look at the following code:
# Include <stdio. h>
Int main ()
{
Const int A = 1;
A = 2;
Printf ("% d/N", );
Return 0;
}
At first glance, you will know that the following error must be reported during compilation: Assignment of read-only variable.
So you know that variable A is defined as const, which is read-only and cannot be changed, but is that true?
See the following code:
# Include <stdio. h>
Int main ()
{
Const int A = 1;
Int * B = &;
* B = 2;
Printf ("% d", A, * B );
Return 0;
}
After compilation, you can guess the result. Let's talk about it through compilation first.
When you set a variable to const, the compiler will pat your chest and tell you that the value of this memory address will not be changed through this variable name, but please note that it can still be changed by any external program or even other names of the same program.
Now let's look at the results of this program:
2 2
In fact, the results of this program are not representative. I use gcc4.2.4, which does not mean that all compilers are like this, which is equivalent to C/C ++ language. Let's take a look at what the C language standards mean:
If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.
That is to say, this is not an issue yet!
So what is the use of const? Since it can also be changed, the results are still unknown.
In brief, there are two main purposes:
1. Prevent the program from accidentally modifying some variables that only need to be accessed.
2. Make the compiler know that this value will not change and it will optimize your program. There are two ways to optimize it: one is to store the variable value in the register at the same time, so that even if * B modifies, when reading its value, it will still read the original value in the register. The second is that it will even replace variable A with a set constant value in all programs where A is used. For example, this program is 1.
Now, you understand. In the future, don't let the brick house (for example, I) go over the ANSI C standard.

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.