# Task Stack allocation problem

**URL:** <https://forums.freertos.org/t/task-stack-allocation-problem/1135>\
**Category:** Kernel\
**Created:** [April 16, 2012, 8:58am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135 "2012-04-16T08:58:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 16, 2012, 8:58am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/1 "2012-04-16T08:58:27Z")

</div>

**[anuradha123](https://sourceforge.net/u/anuradha123/)** wrote on [Monday, April 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#5744):

Hello,

According to documentation individual task stacks are allocated from the Total Heap size we will be define and allocate in FreeRTOSConfig.h.  
Is there any possibility,&nbsp; of the addition of all individual task stacks exceeding the amount of total heap allocation?

Thank you.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 16, 2012, 9:12am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/2 "2012-04-16T09:12:53Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, April 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#1c5d):

> Is there any possibility,&nbsp; of the addition of all individual task stacks exceeding the amount of total heap allocation?

Naturally, yes, there is a limited amount of RAM and a user can attempt to create an infinite amount of tasks.

heap\_1.c and heap\_2.c both have a function xPortGetFreeHeapSize() that allows you to query the stack remaining.&nbsp; heap\_3.c uses malloc() and free() so FreeRTOS itself cannot obtain the free space.&nbsp; There is also a malloc failed hook function that can be defined that will be called when an allocation fails.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 16, 2012, 9:13am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/3 "2012-04-16T09:13:57Z")

</div>

**[anuradha123](https://sourceforge.net/u/anuradha123/)** wrote on [Monday, April 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#7a16):

Further, Can anybody explain how does the allocated Total Heap size is being used?

Are there any documentation to follow up?

Thank you.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 16, 2012, 9:21am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/4 "2012-04-16T09:21:23Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, April 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#d897):

Sorry - I’m not sure I fully understand your question.&nbsp; Have you seen this page:  
[http://www.freertos.org/a00111.html](http://www.freertos.org/a00111.html)

Regards.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [April 16, 2012, 12:13pm UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/5 "2012-04-16T12:13:36Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Monday, April 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#08dc):

anuradha123,  
The total heap size parameter is used in heap\_1 and heap\_2 to define the size of the heap to be created. For heap\_3 you need to use the method provided by your compiler/linker to set this, and the parameter in FreeRTOS is ignored.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 18, 2012, 3:15am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/6 "2012-04-18T03:15:01Z")

</div>

**[anuradha123](https://sourceforge.net/u/anuradha123/)** wrote on [Wednesday, April 18, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#707e):

All,  
Thanks for the replies.  
But I still did not get any answer to my original question,

> Further, Can anybody explain how does the allocated Total Heap size is being used? Are there any documentation to follow up?

Information provided in FreeRTOS site and manual are inadequate.  
I am in interested of finding out, in detail how the task stack is being used.

Thank you.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [April 18, 2012, 6:35am UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/7 "2012-04-18T06:35:35Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, April 18, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#2975):

Forgive us, but your question was not clear.

Each task has its own stack, that is used by the compiler to store things like function call return addresses and local variables.&nbsp; There is nothing different about the stack used by a task than any other stack - for example the stack allocated by the linker for use by the main() function.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 27, 2013, 9:18pm UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/8 "2013-01-27T21:18:20Z")

</div>

**[horaira](https://sourceforge.net/u/horaira/)** wrote on [Sunday, January 27, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#4abe):

> There is nothing different about the stack used by a task than any other stack - for example the stack allocated by the linker for use by the main() function.

Does this means that each task behaves like main (in terms stack uses)&nbsp; that particular task stack also caters the requirement of nested function calls etc.  
Can we get a pictorial depiction of this, some book or online references ?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 27, 2013, 9:20pm UTC](https://forums.freertos.org/t/task-stack-allocation-problem/1135/9 "2013-01-27T21:20:58Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Sunday, January 27, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/32979289/#eb3c):

[http://sourceforge.net/projects/freertos/forums/forum/382005/topic/6687087](http://sourceforge.net/projects/freertos/forums/forum/382005/topic/6687087)
