About Stack and Heap

nobody wrote on Wednesday, January 03, 2007:

Have been struggling with stack and heap for days.
Now just want to some firm answers from somebody.
1) what’s the maximum size of the stack for the task. (when you create a task, you need to pass a number into the function. somebody said this number cannot be more than 255).
2) when the program is running, what is the stack? Is the stack just used for function calls, or all so store all the temperary variables? My understanding is all the temperary variables and the local variables are in heap?
3) The heap and the stack, are they located in different location, or they overlap?

Please help, i am sinking…

rtel wrote on Wednesday, January 03, 2007:

The maximum stack size that can be specified is 0xffff as the parameter in the xTaskCreate() function used to set the stack size is 16bits.   Of coarse you are limited by the amount of RAM available in your system.

Note that the stack is specified as the number of variables that can be saved to the stack - so the number of bytes allocated depends on the size of the stack variables.  For example, on a machine with a 32bit wide stack, requesting a stack size of 100 will result in 32*100 bytes being allocated for the stack.

How the stack is used is dependent on your compiler.  Generally it holds function call return addresses, parameter values, local variables, etc.  Just like any other C program.

The FreeRTOS.org heap is the area of memory you allocated for use by the kernel.  When a task is created the memory used by the task comes from the heap (the task stack and the TCB structure).  Likewise when you create a queue the memory used by the queue comes from the heap.

You may also setup a separate heap for use by your C program through the use of malloc and free library functions.


nobody wrote on Wednesday, January 03, 2007:

Thank you very much Richard!

You are the man.

Your answer is very helpful.

More questions coming… (that’s happens when you are trying to help people … :-D)

I am using LPC2138, with 32K RAM.
I am using heap_1.c
What is the maximum size of heap I can use.
I have 9 tasks running. the total stack size is 40 * 128 * 4 = 20480 bytes.
#define configMINIMAL_STACK_SIZE    ( ( unsigned portSHORT ) 128 )   (portSTACK_TYPE is long , 4 bytes)

Does that mean i just need to set up the configTOTAL_HEAP_SIZE to 20480 bytes?

And what does the RTOS do with the (32K - configTOTAL_HEAP_SIZE)?