如何给一个变量存储在特定内存位置? [英] How to store a variable at a specific memory location?

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

问题描述

由于我是比较新的C,我必须用我的项目下列之一:
我必须声明其必须存储每一个程序在相同的内存地址运行时的一些全局变量。
我做了一些阅读,我发现这就是我把它声明静态它会被存储在相同的存储位置。

As i am relatively new to C , i have to use for one of my projects the following: i must declare some global variables which have to be stored every time the program runs at the same memory address. I did some read and i found that is i declare it "static" it will be stored at the same memory location.

但我的问题是:我可以表明该程序在哪里存储该变量或没有。
例如:int类型的要存放在0xff520000。可这件事情做或没有?我已经在这里搜查,但并没有发现任何相关的例子。如果他们是这方面一些老帖子,请这么好心分享的链接。

But my question is: can i indicate the program where to store that variable or not. For example : int a to be stored at 0xff520000. Can this thing be done or not? i have searched here but did not found any relevant example. If their is some old post regarding this, please be so kind to share the link .

感谢大家提前。
Laurentiu

Thank you all in advance. Laurentiu

更新:我使用的是32uC

Update: I am using a 32uC

推荐答案

在你的IDE会有通过一些连接文件可用内存映射。它将包含在程序中的所有地址。阅读MCU手册,看看在哪些地址没有你的目的有效的内存,那么储备一些内存,为您的变量。你必须看你的具体的开发平台的文档。

In your IDE there will be a memory map available through some linker file. It will contain all addresses in the program. Read the MCU manual to see at which addresses there is valid memory for your purpose, then reserve some of that memory for your variable. You have to read the documentation of your specific development platform.

接下来,请注意,它并没有多大意义,除非它们要么是硬件寄存器或驻留在闪存或EEPROM非易失性变量映射在特定地址的变量。

Next, please note that it doesn't make much sense to map variables at specific addresses unless they are either hardware registers or non-volatile variables residing in flash or EEPROM.

如果这样的内存位置的内容将在执行过程中发生变化,因为它是一个寄存器,或者是因为你的程序包含一个引导程序/ NVM编程算法改变NVM存储单元,则变量的必须的是声明为volatile。否则,编译器将优化后,彻底打破你的code。

If the contents of such a memory location will change during execution, because it is a register, or because your program contains a bootloader/NVM programming algorithm changing NVM memory cells, then the variables must be declared as volatile. Otherwise the compiler will break your code completely upon optimization.

在特定的编译器很可能有一个非标准的方式来分配特定地址的变量,例如一个或的#pragma有时怪异,非标 @ 运营商。您可以分配在标准C固定位置的变量的唯一明智的方法,是这样的:

The particular compiler most likely has a non-standard way to allocate variables at specific addresses, such as a #pragma or sometimes the weird, non-standard @ operator. The only sensible way you can allocate a variable at a fixed location in standard C, is this:

#define MY_REGISTER (*(volatile uint8_t*)0x12345678u)

在这里为0x12345678就是那1个字节所在的地址。一旦你有这样一个宏声明,你可以使用它,如果它是一个变量:

where 0x12345678 is the address where 1 byte of that is located. Once you have a macro declaration like this, you can use it as if it was a variable:

void func (void)
{
  MY_REGISTER = 1;  // write
  int var = MY_REGISTER;  // read
}

通常,你期望这类变量驻留在全局命名空间,因此宏。但是,如果你因为某种原因想要的变量的范围会有所减少,然后跳过宏观和手动访问code里面的地址:

Most often you want these kind of variables to reside in the global namespace, hence the macro. But if you for some reason want the scope of the variable to be reduced, then skip the macro and access the address manually inside the code:

void func (void)
{
  *(volatile uint8_t*)0x12345678u = 1; // write
  int var = *(volatile uint8_t*)0x12345678u; // read
}

这篇关于如何给一个变量存储在特定内存位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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