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

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

问题描述

一个变量位于内存中的地址 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天全站免登陆