# Task's Local variables

**URL:** <https://forums.freertos.org/t/tasks-local-variables/6071>\
**Category:** Kernel\
**Created:** [July 23, 2016, 5:51pm UTC](https://forums.freertos.org/t/tasks-local-variables/6071 "2016-07-23T17:51:54Z")\
**Posts on this page:** 5\
**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:** [July 23, 2016, 5:51pm UTC](https://forums.freertos.org/t/tasks-local-variables/6071/1 "2016-07-23T17:51:54Z")

</div>

**[michaeln32](https://sourceforge.net/u/michaeln32/)** wrote on [Saturday, July 23, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/5f9089d9/#fed2):

Hi

I am using MCU STM32F205RC.  
Compiler: I am using Atollic  
FreeRTOS V8.2.1

1.Is a local variable of task remains his value after context switch to another task and back or the local variable of a task reinitilized to his original value ?

2.Is there any common sense to define local variable of a task as static ?

1. Can you please tell me how can i know what is the total maximum size in bytes of tasks stacks ?

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 23, 2016, 7:44pm UTC](https://forums.freertos.org/t/tasks-local-variables/6071/2 "2016-07-23T19:44:22Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Saturday, July 23, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/5f9089d9/#e70a):

FreeRTOS is a proper preemptive multi tasking system so each task is like its own program. Tasks dont know anything about context switches, they don’t start and stop so variables always have the same value, there is no point at which they could reset to their init value.

---

<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:** [July 23, 2016, 10:07pm UTC](https://forums.freertos.org/t/tasks-local-variables/6071/3 "2016-07-23T22:07:21Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Saturday, July 23, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/5f9089d9/#e33b):

The one thing that has problems with automatic (stack based) variables retaining values would be inside co-routines which are a very striped down version of tasks for use in very memory limited machines. As MEdwards states, tasks have no problem retaining values over a context switch.

A Task would have problems remembering values if the task ends and gets restarted, then variables on the stack would likely lose their value.

As far as when to use the stack and when to use local static variables, if the function is used for only 1 task, it makes little difference. If the function is used for multiple tasks, like the function supports multiple devices in the system, and the parameter passed on task startup defines which one that task is handling, then stack variable would be distinct for each class, while a static variable would be shared.

Which is better? Using stack variables makes it easier if later you want to reuse the function for a second task. Static variables make it easier for sub-functions to access the values.

As to the maximum size of the tasks stack, one answer is how much you allocated, as that is the most it is allowed to use. As to how much it will use, some compilers have an option to try and compute the stack usage for a function (and thus a task), others do not, and you would have to estimate/count how much stack is used for variables and add for parameter/function calls and other overhead.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 24, 2016, 6:24am UTC](https://forums.freertos.org/t/tasks-local-variables/6071/4 "2016-07-24T06:24:51Z")

</div>

**[michaeln32](https://sourceforge.net/u/michaeln32/)** wrote on [Sunday, July 24, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/5f9089d9/#8fde):

Thank You very much for your great answers.

the next define is defined in my FreeRTOSConfig.h header file :

define configTOTAL\_HEAP\_SIZE ( ( size\_t ) (15 \* 1024 ) )

Is this define represent the maximum bytes that all the tasks in the system can allocat to them selfs ?

---

<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:** [July 24, 2016, 6:55am UTC](https://forums.freertos.org/t/tasks-local-variables/6071/5 "2016-07-24T06:55:52Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, July 24, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/5f9089d9/#8fde/e727):

configTOTAL\_HEAP\_SIZE is the ‘TOTAL’ available in the entire system -  
not to each task individually. An individual task can allocate as much  
as it likes until the heap is exhausted (no free heap available).  
[http://www.freertos.org/a00111.html](http://www.freertos.org/a00111.html)
