Embedded and C language

Source: Internet
Author: User

Volatile

The meaning of volatile English words is unstable and capricious. The variable is declared in the C language with the volatile keyword, which means that the variable will change unexpectedly and needs to be read again each time it is used. It should be accurate to say that the optimizer uses this variable every time it needs to read the value again, instead of the variable stored in the register. This is a side effect of the compiler optimizer, so some variables are defined as volatile, especially in embedded programming, which is the most basic distinction between embedded engineers and C-language engineers.

What kind of variable does it need to be a volatile type?

    1. Hardware Registers
    2. Non-automatic variables accessed by interrupt handlers (static variables, global variables)
    3. Multi-threaded Common access variables

A little more in-depth talk about some of the first few questions

    1. Can a variable be both volatile and const? Why?
    2. Can the pointer make the volatile? Why?

In the first question, say the keyword const

Const: The average person directly says it is a constant, and a more precise argument should be read-only, and many functions pass in a const type, which prevents you from changing it.

The role of the const:

    1. One parameter is const to tell the user the purpose of this parameter, to convey useful information to the person reading the code.
    2. Reasonable use of Const allows the compiler to naturally protect the parameters that you do not want to change, prevent them from being unintentionally modified, and reduce the occurrence of bugs

Back to the point: the answer is yes, a variable can be both const and VOLATILE,CONST refers to the program should not modify it, volatile is to indicate that the variable may be unexpected changes. A read-only register can be an example of this.

Can a pointer make a volatile one? Obviously yes, it's just not very common, the pointer may change unexpectedly, for example, a pointer to buffer is modified in the interrupt program


A classic face question.

int square (volatile int *ptr) {    return *ptr * *PTR;}
Is there a problem with the above function? If there is, where is wrong, how to change?

The idea of the square function is to ask for the square of a number, but *ptr may have a change of intent, resulting in a different value two reads, such as the value of a hardware register, then the function returns a number of squares.

The code generated for the above function compiler is similar to the

int square (volatile int *ptr) {    int A, b;    A = *ptr;    b = *ptr;    return a * b; A is the first read value, B is the value to read for the second time}

The correct code should be

int square (volatile int *ptr) {    int a = *ptr;    Return a * A;}




Embedded and C language

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.