Forum Discussion
5 Replies
- Altera_Forum
Honored Contributor
The required stack and heap size strongly depend on what your code does and how it is structured.
Stack size usually increase if you use many levels of function calls (main calls fn1 which calls fn2 which calls fn3...) and/or if you use a lot of local variables. This is because each function call pushes some data on the stack, i.e. function parameters, return address, local variables: then the stack grows momentarily until the function returns. More nested calls you have, more the stack will grow. For the same reason the grow could be 'explosive' if you use recursive calls. Heap size substantially depends on manually allocated variables. If your code doesn't use functions like malloc or calloc, you virtually don't need heap at all. Clearly, you must also consider any library functions you are using. They will add some stack -and possibly heap- usage to that stricly required by your code. - Altera_Forum
Honored Contributor
thank you Cris72
so if the stack + heap is not big enough, will the eclipse give me some warnings? - Altera_Forum
Honored Contributor
AFAIK you'll get no warnings.
The compiler can hardly infer from your code how much space is actually required. - Altera_Forum
Honored Contributor
Most likely the stack will crash into the heap and all hell will break loose.
Maximum stack use is likely to be in some obscure error path. Maximumum heap use is likely to happen due to heap fragmentation. In reality, unless you have a lot of spare memory (so it just isn't going to be a problem), you don't want to be using heap allocated memory (certainly not after any initialisation phase). My nios code (which implements a hdlc and a protocol over it) is carfully written so that the compiler can inline all function calls and never uses the stack at all (ok, there is still some function prologue that saves registers on the stack). - Altera_Forum
Honored Contributor
If you are really concerned about stack, you should check the "enable_runtime_stack_checking" option at the nios2-bsp-editor (Main>>Advanced>>hal).
This however can increase code size but at least will guarantee stack corruption does not occur.