Embedded and C Language

Source: Internet
Author: User

Embedded and C Language

Volatile

Volatile English words mean unstable and capricious. in C language, the volatile keyword is used to declare a variable. That is to say, this variable will undergo unexpected changes and needs to be read again every time this value is used. precisely speaking, the optimizer needs to re-read this value every time when using this variable, rather than using the variable stored in the register. This is a side effect of the compiler optimizer, so some variables should be defined as volatile, especially in embedded programming, which is the most basic difference between embedded engineers and C language engineers.

What kind of variables need to be volatile?

Non-automatic variables (static variables, global variables) for hardware register interrupt handler access

Let's talk about it in a bit more detail. I 'd like to raise a few questions first.

Can a variable be volatile or const? Why? Can the pointer make volatile? Why?

When talking about the first question, let's first talk about the keyword const.

Const: Generally, it is a constant. A more precise description should be read-only. Many functions pass in a const type to prevent you from changing it.

The role of const:

A const parameter is used to tell users the purpose of this parameter, sending useful information to the code reader using const can enable the compiler to naturally protect parameters that do not want to be changed, prevent them from being accidentally modified, and reduce the appearance of bugs.

Back to the question: the answer is yes. A variable can be both const and volatile. const means that the program should not modify it. volatile indicates that the variable may change unexpectedly. A read-only register can be used as an example.

Can one pointer make volatile? Obviously yes, it is not very common. The pointer may be unexpectedly changed. For example, the interrupt program modifies a pointer pointing to the buffer.


A classic interview question

int square(volatile int *ptr){    return *ptr * *ptr;}
Is there a problem with the above function? If yes, where is the error? How can this problem be solved?

The idea of the square function is to calculate the square of a number. However, * ptr may not change the intended value, resulting in two different read values, such as the value of the hardware register, then this function does not return the square of a number.

The code generated by the above function compiler is similar

Int square (volatile int * ptr) {int a, B; a = * ptr; B = * ptr; return a * B; // a is the first read value, B is the second read value}

The correct code should be

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




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.