Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Controlling stack & Heap size

Altera_Forum
Honored Contributor II
2,947 Views

Hello, 

 

I'm using NIOS II EDS (eclipse). I can't find where in the EDS I can define the stack & the heap size of my project. Can you help? 

 

Thanks, 

 

Yehuda.
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
1,730 Views

The stack and heap are dynamic so they grow and shrink during run time. For example when you call a function variables get placed on the stack so that when you return from the function they are still present. If you use global variables or use malloc() then the heap is used for that. (I'm over simplifying it so these are just examples) The stack starts at one location and the heap starts at another, and as the usage of each grows they get closer together (if they are located sequentially in memory) and if they get too large you can run into a "collision" and data corruption will result. 

 

So the IDE can't tell you how big they are since it has no clue how your code behaves. This is true of any processor and compiler. To find out more search for "heap" or "stack" in this document: http://www.altera.com/literature/hb/nios2/n2sw_nii5v2.pdf
0 Kudos
Altera_Forum
Honored Contributor II
1,730 Views

BadOmen, 

 

Thanks for your reply. All you said is correct and I'm familiar with it. 

 

What I ment is the stack size that the linker is pre-allocating before "C" is starting to run. This is a "hole" in the memory that linker keeps for the stack so the linker will be able to use the rest of the memory for other things. 

 

This parmeter exist in any other platform I know and especially per task in OS like uC/OS-II. 

 

Same for heap. 

 

Yehuda.
0 Kudos
Altera_Forum
Honored Contributor II
1,730 Views

Look at the linker script, and/or the asm code that calls alt_main (which will setup %sp). 

 

At a guess, the stack starts at the top of physical memory (the main memory block), and the heap just after the BSS. 

I use a custom build - which doesn't have a heap and initialieses %sp (and %gp) from code added directly by the linker script (in hex). 

(Actually I only need a stack because gcc insists on generating a function prologue! and won't let me reassign %sp as a general purpose register!)
0 Kudos
Altera_Forum
Honored Contributor II
1,730 Views

Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer. More about....stack and heap 

 

(http://net-informations.com/faq/net/stack-heap.htm)Brian
0 Kudos
Reply