3

I am programming for an embedded system, where resources are at premium cost.

A few techniques that I am aware of are -

  1. Remove/Reduce unused variable/memory.
  2. Use macros for small/inline functions.

Is there anything else that I can use for this? Also, please correct if I am wrong.

Please Note: I am not asking exclusively about low level programming.

Let's say I am bound to use some particular amount of stack memory only. My program doesn't use recursion.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
µtex
  • 900
  • 5
  • 12
  • _out of run time memory_: you should elaborate a bit. Which memory, which section? – LPs Jun 20 '16 at 09:44
  • 2
    Please be more specific. For example: are you using some recursion? And macro does not change anything for saving stack place. – Boiethios Jun 20 '16 at 09:48
  • If you are runnging out of stack, point 2 is useless.. – LPs Jun 20 '16 at 09:50
  • No I am not using any recursion. – µtex Jun 20 '16 at 09:51
  • @LPs.. correct. Thanks – µtex Jun 20 '16 at 09:52
  • 3
    So without recursion running out of stack can happen if you have a tons of nested call to functions or if you declare some "huge" array/matrix/structs with local scope in function. Is it the case? – LPs Jun 20 '16 at 09:55
  • @LP.. Yes. HAD lot of nested call to functions/structs. So applied above two things. Is there anything else I can try. – µtex Jun 20 '16 at 09:58
  • 1
    @sas `malloc` more things instead of having array or structs in the stack. – Boiethios Jun 20 '16 at 10:00
  • Not an answer, but I hope you are already using something like [this](http://stackoverflow.com/q/6387614/509868). If not, please do - it will help you see where to do optimizations, instead of trying blindly. – anatolyg Jun 20 '16 at 10:00
  • 5
    The question is too broad; A problem like this is very specific to your application and target. For example you may have run out of stack, but do you also have a heap allocation and space remaining for static allocation? Can you simply increase the stack size or have you exhaused *all* available memory (in which case it is not a question just about stack usage). In the end memory efficient design and implementation of data structures is your answer, but you have not provided any information about your application to allow advice on that. – Clifford Jun 20 '16 at 10:03
  • @Clifford.. I can't increase my stack size. Yes heap is remaining. Thanks for pointing out about heap.. – µtex Jun 20 '16 at 10:09
  • @Boiethios.. Thanks for pointing out about heap. – µtex Jun 20 '16 at 10:11
  • 2
    Using dynamic allocation on embedded can be not the right choice. You can find tons of posts on the web that explain memory fragmentation and other stuff. Take it carefully. – LPs Jun 20 '16 at 10:26
  • look at the kinds of things that consume stack, but also balance the tradeoff, more code or more memory elsewhere. the more functions you have the more stack frames the more stack used. The more local variables you use the more stack you use. optimization will take care of some of this. globals can help memory consumption, but can also hurt depending on how often used and how many copies of the variable they are replacing across the function calls. letting the compiler optimize is your first attack on stack/memory use as many locals will just sit in registers instead. – old_timer Jun 20 '16 at 11:48
  • for memory conservation also use static on all functions that are not global, the compiler doesnt know and will include the function for other objects, but may inline the function in the code in the same source file, basically having a public function consuming flash or ram depending for no reason. this probably is just a flash saving exercise, but it depends on your system and how you run it. – old_timer Jun 20 '16 at 11:49
  • letting the compiler optimize is your primary tool to attack stack consumption. but it is not as deterministic, add one line of code and your stack consumption can see a noticeable change, it depends on the code. – old_timer Jun 20 '16 at 11:50
  • My point about increasing stack size was rhetorical. It was intended to highlight the deficiencies in the question. You should fix the question rather than starting a dialogue in comments. Explain for example why you cannot increase stack, (possibly reducing heap to accommodate it. – Clifford Jun 20 '16 at 12:57

0 Answers0