如何定义一个描述内存位置的宏? [英] How to define a macro describing a memory location?

查看:54
本文介绍了如何定义一个描述内存位置的宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

变量位于地址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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆