Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Valued Contributor III
1,274 Views

how much space should i give to the stack + heap in nios?

hi  

in nios software, after i build it, the message shows: 

Info: (vi23_swprj.elf) 23 KBytes program size (code + initialized data). 

Info: 6764 Bytes free for stack + heap. 

 

how much space should i give to the stack + heap? 

how could i decide the stack + heap size?
0 Kudos
5 Replies
Highlighted
Valued Contributor III
37 Views

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.
0 Kudos
Highlighted
Valued Contributor III
37 Views

thank you Cris72 

so if the stack + heap is not big enough, will the eclipse give me some warnings?
0 Kudos
Highlighted
Valued Contributor III
37 Views

AFAIK you'll get no warnings.  

The compiler can hardly infer from your code how much space is actually required.
0 Kudos
Highlighted
Valued Contributor III
37 Views

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).
0 Kudos
Highlighted
Valued Contributor III
37 Views

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.
0 Kudos