# Persistence of task variable

**URL:** <https://forums.freertos.org/t/persistence-of-task-variable/11962>\
**Category:** Kernel\
**Created:** [March 11, 2021, 10:14pm UTC](https://forums.freertos.org/t/persistence-of-task-variable/11962 "2021-03-11T22:14:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [March 11, 2021, 10:14pm UTC](https://forums.freertos.org/t/persistence-of-task-variable/11962/1 "2021-03-11T22:14:15Z")

</div>

Hi,  
are local variables defined inside a task handler function persistent across multiple calls (i.e. as if they were defined as static)?

I assumed I need to define them as static if I wanted that persistence (like everywhere else in c) but perhaps the freeRTOS has some precompile directives that change that? I am asking because came across a post in a forum where they were storing the value across multiple invocations which seems strange. Or was it just wrong code?

Thank you

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [March 11, 2021, 10:24pm UTC](https://forums.freertos.org/t/persistence-of-task-variable/11962/2 "2021-03-11T22:24:41Z")

</div>

I’m not sure what you mean…  
Variables are variables and their persistence / visibility is defined by the C/C++ standard. There is no hidden FreeRTOS magic.  
The only basic thing managed by a multitasking OS like FreeRTOS are separate stacks for each task but in a transparent way and you don’t need to worry about. They’re just normal stacks.

---

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [March 11, 2021, 10:26pm UTC](https://forums.freertos.org/t/persistence-of-task-variable/11962/3 "2021-03-11T22:26:01Z")

</div>

Then that’s what I thought, just wrong coding of that example.

Thank you Hartmut

---

<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:** [March 12, 2021, 2:23am UTC](https://forums.freertos.org/t/persistence-of-task-variable/11962/4 "2021-03-12T02:23:36Z")

</div>

If the task is being created from a static TCB and Stack buffer, you may well get the appearance of value persisting between recreations of the task, but that is not promised. (I think you will need to turn off stack checking, or that code will clear the stack frame and wipe out the old information.

I presume you aren’t just mistaking a task having a loop, and the values being preserved between loops.

---

<div class="post-metadata">

**Author:** ![Rik001](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rik001](https://forums.freertos.org/u/Rik001)\
**Post date:** [March 12, 2021, 2:49am UTC](https://forums.freertos.org/t/persistence-of-task-variable/11962/5 "2021-03-12T02:49:03Z")

</div>

Thank you Richard,  
no the task does not have a loop inside it. I think it was just a wrong piece of code.
