The difference between const and volatile

Source: Internet
Author: User

I. What does the keyword const imply?

As soon as he hears, "const means constant," I know I'm dealing with an amateur. Last year Dan Saks has completely summed up all the usages of const in his article, so ESP (translator: Embedded Systems programming) Every reader should be very familiar with what const can and cannot do. If you've never read that article, you can just say that const means "read-only". Although this answer is not a complete answer, I accept it as a correct answer. (If you want to know more about the answer, read Saks's article carefully.) )
If the candidate can answer the question correctly, I will ask him an additional question:
What does the following statement mean?

const int A;
int const A;
const int *a;
int * const A;
int const * const A;

The first two functions are the same, a is a constant integer number. The third means that a is a pointer to a constant integer number (that is, the integer number is not modifiable, but the pointer can). The fourth meaning a is a constant pointer to an integer number (that is, the integer number pointed to by the pointer can be modified, but the pointer is non-modifiable). The last one means that a is a constant pointer to a constant number (that is, the integer number pointed to by the pointer is not modifiable and the pointer is not modifiable). If the candidate can answer these questions correctly, then he leaves a good impression on me. Incidentally, perhaps you may ask, even without the keyword const, it is easy to write the function of the correct program, then why do I value the keyword const? I also have the following several reasons:
?; The function of the keyword const is to convey very useful information to the person who is reading your code, and in fact, declaring a parameter as a constant is to tell the user the purpose of the parameter's application. If you have spent a lot of time cleaning up the rubbish left by others, you will soon learn to thank for the extra information. (Of course, it's very rare for a const programmer to leave rubbish for others to clean up.) )
?; By giving some additional information to the optimizer, using the keyword const may produce more compact code.
?; Proper use of the keyword const allows the compiler to naturally protect those parameters that you do not want to change, and prevent them from being unintentionally modified by the code. In short, this can reduce the occurrence of bugs.

Second, volatile

volatile The result of the compiler compilation, refers to the volatile variable is at any time possible changes, volatile variables related to the operation, do not compile optimization, to avoid errors, (VC + + in the production of release version executable code will be compiled optimization, Variables related to the volatile keyword will not be optimized for compilation. )。

For example:
volatile int i=10;
int j = i;
...
int k = i;

Volatile tells the compiler that I can change at any time, and each time it is used it must be read from the address of I, so the compiler generated executable code will re-read the data from I's address in K. The optimization approach is that since the compiler found that the code between the I-read data two times does not operate on I, it automatically places the last-read data in K. Instead of re-reading from inside I. Thus, if I is a register variable or represents a port data is prone to error, so volatile can guarantee a stable access to the special address, no error.

The difference between const and volatile

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.