工程師應知道的0x10個問題(8): Volatile的用法

MuLong PuYang
2 min readFeb 28, 2022

--

英文參考網址

A ‘C’ Test: The 0x10 Best Questions for Would-be Embedded Programmers

中文參考網址

C語言測試 應知道的0x10個基本問題

原文翻譯

8. What does the keyword volatile mean? Give three different examples of its use.

Keyword volatile代表什麼? 給出三個它的使用的不同範例

A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are:

一個 volatile 變數會被不可預期的改變。因此,編譯器可以使這個變數沒有假設。具體來說,編譯器會小心的重載這個變數當這個變數每次被使用時,而不是保存一份拷貝在編譯器中。Volatile變數的範例如下:

(a) Hardware registers in peripherals (e.g., status registers)

(a) 周邊設備的硬體戰存器 (如: 狀態戰存器)

(b) Non-stack variables referenced within an interrupt service routine.

(b) 中斷程式routine會訪問到的 Non-statck 變數

注: C語言測試 應知道的0x10個基本問題 這邊寫的是Non-automatic variables而不是Non-stack變數
A C Test: The 0x10 Best Questions for Would-be Embedded Programmers 這一篇也是寫Non-automatic variables

(c) Variables shared by multiple tasks in a multi-threaded application.

(c) 多執行緒應用中多個任務共享的變數

If a candidate does not know the answer to this question, they aren’t hired. I consider this the most fundamental question that distinguishes between a Non-automatic variables and an ‘embedded systems programmer’. Embedded folks deal with hardware, interrupts, RTOSes, and the like. All of these require volatile variables. Failure to understand the concept of volatile will lead to disaster.

如果面試者不知道如何回答這個問題,他們不會被聘僱。我認為這是個基礎的問題可以來區分一個C語言程式設計師以及一個嵌入式系統程式設計師。嵌入式人員時常要處理硬體、中斷、RTOSes之類的問題。這些都需要volatile變數。不懂volatile概念的話會導致災難。

On the (dubious) assumption that the interviewee gets this question correct, I like to probe a little deeper, to see if they really understand the full significance of volatile. In particular, I’ll ask them the following:

假設這些應試者真的答對了這題(半信半疑的),我會探查更深一點,來看他們是否真的完全理解 volatile 的重要性。具體來說,我會問他們以下這些問題

(a) Can a parameter be both const and volatile? Explain your answer.

(a) 一個 parameter 是否可以同時為 const 也可以為 volatile? 解釋你的答案

(b) Can a pointer be volatile? Explain your answer.

(b) 指標可以是 volatile? 解釋你的答案

(c)What is wrong with the following function?:

(c) 下面這個函數有什麼問題?

int square(volatile int *ptr)

{

return *ptr * *ptr;

}

The answers are as follows:

(a) Yes. An example is a read only status register. It is volatile because it can change unexpectedly. It is const because the program should not attempt to modify it.

(a) 是的。一個範例是一個唯獨的 status register。它是 volatile 因為它可以不可預期的改變。它是 const 因為程式不應該企圖改變它。

(b) Yes. Although this is not very common. An example is when an interrupt service routine modifies a pointer to a buffer.

(b) 是的。雖然這並不普遍。一個例子是中斷服務routine改變一個指向 buffer 的指標

(c)This one is wicked. The intent of the code is to return the square of the value pointed to by *ptr. However, since *ptr points to a volatile parameter, the compiler will generate code that looks something like this:

(c) 這個是很壞的。這個程式的意圖是為了回傳被 *ptr 指到的數的平方。然而,因為 *ptr 指到的是 volatile parameter,編譯器會產生看起來像是以下的程式碼

int square(volatile int *ptr)

{

int a,b;

a = *ptr;

b = *ptr;

return a * b;

}

Since it is possible for the value of *ptr to change unexpectedly, it is possible for a and b to be different. Consequently, this code could return a number that is not a square! The correct way to code this is:

因為 *ptr 有可能不可預期的改變,很有可能 a 與 b 會是不同的。所以,這個程式碼可能會回傳布是平方的數值。正確寫法的程式碼應該要像是:

long square(volatile int *ptr)

{

int a;

a = *ptr;

return a * a;

}

--

--