如何定义描述内存位置的宏? [英] How to define a macro describing a memory location?
问题描述
一个变量位于内存中的地址 0x10000.如何定义一个宏,以便写入该宏会写入该内存位置?
A variable is located in memory at address 0x10000. How can I define a macro such that writing to that macro writes to that memory location?
推荐答案
在 C 中声明可写内存位置的事实标准方法是这样的:
The de facto standard way of declaring a writeable memory location in C is this:
#define REGISTER (*(volatile uint8_t*)0x10000)
其中 uint8_t
应对应于该内存位置的内容大小.
where uint8_t
should correspond to the size of the contents at that memory location.
然后 REGISTER = something;
写入该内存位置,就像 REGISTER 是一个变量一样.
And then REGISTER = something;
writes to that memory location, just as if REGISTER was a variable.
使用 volatile
关键字很重要,以防止优化错误并确保对位置的读取始终是最新的.
It is important to use the volatile
keyword, to prevent optimization bugs and ensuring that reads of the location are always up to date.
这篇关于如何定义描述内存位置的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!