问题描述
变量位于地址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.
这篇关于如何定义一个描述内存位置的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!