简体   繁体   English

C中的RAM优化:堆栈还是全局变量?

[英]RAM optimization in C : stack or global variable?

I am adding (yet) another sub-module to my fairly large project written in C, which I want to run on my little processor that I have. 我向我用C编写的相当大的项目中添加了(但)另一个子模块,我想在我拥有的小处理器上运行它。 And now I wonder : my new module my be small, but it requires to pile up quite some intermediary information every cycle in order to compute its stuff. 现在我不知道:我的新模块很小,但是它需要在每个循环中积累一些中间信息才能计算其内容。 I created a data structure to hold all this intermediary data, and that's what I wonder about : 我创建了一个数据结构来保存所有这些中间数据,这就是我想知道的:

  • I could simply create a global instance of this data structure in my module source file, available at all times from everywhere in my module. 我可以简单地在模块源文件中创建此数据结构的全局实例,该模块随时随地都可以在模块中的任何位置使用。 But this means a constant RAM occupation of the size of this intermediary data, even when other modules are running. 但这意味着即使在运行其他模块时,该中间数据的大小也会被RAM占用。 That's what I originally had, and that cannot be optimal in any way. 那就是我最初拥有的,无论如何都不是最优的。
  • I could create a local instance of this data structure at the beginning of my update cycle, and pass a pointer to it all the time to each function. 我可以在更新周期开始时创建此数据结构的本地实例,并始终将指向它的指针传递给每个函数。 But it's horrible from a coding point of view (long function prototypes), and it means everytime I call another function, I have to add another copy of that pointer on top of the stack (right ?) 但这从编码的角度来看(长函数原型)是可怕的,这意味着每次我调用另一个函数时,都必须在堆栈顶部添加该指针的另一个副本(对吗?)
  • I could also create a global variable (scoped to my module) which is simply a pointer to my intermediary data. 我还可以创建一个全局变量(作用于我的模块),该变量只是指向我的中间数据的指针。 This means one 8-bit integer constantly there (even when all modules are running), but not more. 这意味着一个恒定的8位整数(即使所有模块都在运行),但不会更多。 And then the data is accessible from everywhere in the module without stack overhead and long function prototypes. 然后,可以从模块中的任何位置访问数据,而无需堆栈开销和长函数原型。

Some info as to my system : modules run one after the other, re-entrance only occurs to get results, but not perform further calculations. 关于我的系统的一些信息:模块一个接一个地运行,重新进入只是为了获得结果,而不执行进一步的计算。 Intermediary data is really only needed during the update calls of each module, and those always come in the same order. 实际上,仅在每个模块的更新调用期间才需要中间数据,并且中间数据始终以相同的顺序出现。

Which solution is best ? 哪个解决方案最好? In which light ? 在哪个光线下? Where does the compiler come into play ? 编译器在哪里发挥作用? Is there any general memory mechanism I do not know about here ? 这里有我不知道的任何通用存储机制吗?

Thanks in advance for the clarifications ! 预先感谢您的澄清!

This depends quite a lot on what kind of system you are working with, and what's most important, execution speed or memory consumption. 这在很大程度上取决于您所使用的系统类型,最重要的是执行速度或内存消耗。 I would assume that "little processor" means a microcontroller. 我认为“小处理器”是指微控制器。 You don't mention re-entrancy so I'll assume that's not an issue. 您没有提到重新进入,所以我认为这不是问题。

In that case, you are just fooling yourself if you think that having a file scope struct wastes memory. 在这种情况下,如果您认为拥有文件作用域结构会浪费内存,那您就在自欺欺人。 Because you always have to design for the worst case, and the worst case here will be when a function is called that needs to use the struct. 因为您总是必须针对最坏的情况进行设计,而最坏的情况是调用函数时需要使用该结构。 At that time, your total, worst-case memory consumption will involve the struct, and it will always be the same no matter if the struct is allocated at file scope or on the stack. 那时,您的最坏情况下的总内存消耗将涉及该结构,并且无论该结构是在文件作用域还是在堆栈上分配,它始终是相同的。

If you don't have enough memory to handle the worst case, then no allocation method in the world will save you. 如果您没有足够的内存来处理最坏的情况,那么世界上没有任何分配方法可以拯救您。 It doesn't make any sense to "temporarily save some memory". “临时保存一些内存”没有任何意义。 Saving some memory when not running the worst case is irrelevant. 不运行最坏情况时节省一些内存是无关紧要的。 You will still need to have enough memory for the worst case - either you have this or you don't. 对于最坏的情况,您仍然需要有足够的内存-无论您拥有还是没有。

Therefore, the best option is to leave the struct at file scope, because that doesn't affect memory consumption but saves execution time. 因此,最好的选择是将结构保留在文件范围内,因为这不会影响内存消耗,但可以节省执行时间。

If your module needs to be thread-safe or allow multiple instances, then that's another story entirely. 如果您的模块需要线程安全或允许多个实例,那么这完全是另一回事了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM