不要职能中的变量坚持在RAM上Arduinos任何时候? [英] Do variables within functions persist in RAM at all times on Arduinos?

查看:164
本文介绍了不要职能中的变量坚持在RAM上Arduinos任何时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相当的是推动处理器上可用的内存限制的大Arduino的草图。正如我在释放内存和减少我的使用全局变量尽可能。我使用 MemoryFree.h (详细信息,可以发现的这里)检查可用内存。我有一个工作草图(太长,张贴在这里),但是当我添加另一个函数与几个变量(不到什么,我认为可用内存),我的系统崩溃或者停止由于没有记忆。

I have quite a big Arduino sketch that is pushing the limits of available memory on the processor. As is I'm freeing up memory and reducing my use of global variables as much as possible. I'm using MemoryFree.h (details can be found here) to check available memory. I have a working sketch (too long to post here) but when I add another function with a few variables (that are within what I consider available memory) my system crashes or halts due to no memory.

我增加了以下功能:

boolean moved(){

int yreadings[4];
int zreadings[4];


free(&yreadings);
free(&zreadings);
}

然后我加入到我的主循环使用code:

I then added to my main loop the following code:

 Serial.print("Mem is ");
 Serial.println(freeMemory());
 moved();

如果移动()在环路和整个功能如下注释掉我得到的输出

If moved() in the loop and the entire function below are commented out I get an output of

Mem is 499

在每次迭代,提示没有的每次迭代的记忆丧失。

on each iteration, suggesting no loss of memory per iteration.

然而,如果我取消的功能和它我得到以下输出调用:

Yet if I uncomment the function and the calling of it out I get the following output:

Mem is 499
Mem is -16094

上崩溃之前,第一和第二行...

on the first and second lines before crashing...

即使内存是持续性的,不应这只用远远低于每次迭代整个499个字节?但是,我怎么失去任何内存呢?

Even if memory is persistant shouldn't this only use much less than the entire 499 bytes per iteration? Still, how am I losing ANY memory at all?

更新:
更为奇特:如果删除移动()函数并声明整型数组全局我仍然得到499内存readins怎么会这样呢?难道不应该由存储器24整数消费量减少?

UPDATE: Even more strange: if I remove the moved() function and declare the integer arrays globally I STILL get memory readins of 499. How is that so? Shouldn't it be decreased by the amount of memory 24 integers consume?

推荐答案

如果你需要自动存储只有函数的执行部分存在,你可以把它们的作用域块内:

If you need automatic storage to exist for only part of the execution of a function you can place them inside a scope block:

void foo(void) {

   // some code where neither reading variable is occupying memory

   {
      int yreadings[4];
      int zreadings[4];

      // here these variable are taking up memory

   }

   // other code where neither reading variable is occupying memory
}

当然你只能在该块中使用它们。

but of course you can only use them in that block.

要扩大我的评论有三类内存使用具有不同持续时间

To expand on my comment there are three classes of memory use that have different durations


  • 全局和函数或方法或类上下文变量声明静态该程序的整个生命存在

  • 自动变量(函数或方法中的声明)其范围包括该范围
  • 调用的任何其他子程序的生命的生存期存在
  • 动态分配是由 1 页头家庭获得的内存块的功能(这将永远是比你记账的原因要求尺寸大一点),这从时间坚持你分配,直到你叫删除免费(分别)。

  • Globals and variables declared static in function or method or class context exist for the entire life of the program
  • Automatic variables (those declared inside a function or method) exist for the lifetime of their scope including the life of any other subprograms called from that scope
  • Dynamic allocations are blocks of memory obtained from new1 or the alloc family of function (which will always be a bit larger than the size you request for bookkeeping reasons) which persist from the time you allocate them until you call delete or free (respectively).

1 如果重写这可能会或可能不会仍然适用,但如果这样做,这是你的注意去了解你已经做了自己。

1 If you override new this may or may not still apply, but if you do that it is your lookout to understand what you've done to yourself.

这篇关于不要职能中的变量坚持在RAM上Arduinos任何时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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