工程師應知道的0x10個問題(10): 存取固定的記憶體位置

MuLong PuYang
1 min readMar 5, 2022

--

英文參考網址

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

中文參考網址

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

原文翻譯

10. Embedded systems are often characterized by requiring the programmer to access a specific memory location. On a certain project it is required to set an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.

嵌入式系統常有一個特點是要求程式設計失去存取特定的記憶體位置。在某個專案中被要求設定一個絕對位址在0x67a9的整數變數為數值0xaa55。編譯器是一個純ANSI編譯器。寫下程式碼來完成這個任務。

This problem tests whether you know that it is legal to typecast an integer to a pointer in order to access an absolute location. The exact syntax varies depending upon one’s style. However, I would typically be looking for something like this:

這個問題測試你是否知道這是合法的去型別轉換一個整數成一個指標為了存取一個絕對位置。確切的語法根據每個人的風格因人而異。然而,我典型的找像是這個:

int *ptr;

ptr = (int *)0x67a9;

*ptr = 0xaa55;

A more obfuscated approach is:

一個更混淆的方法是:

*(int * const)(0x67a9) = 0xaa55;

Even if your taste runs more to the second solution, I suggest the first solution when you are in an interview situation.

即使你的品味更偏向第二種方案,我建議第一個方案當你在面試的時候

--

--