# nested function calls and memory usage

**URL:** <https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233>\
**Category:** Kernel\
**Created:** [January 9, 2013, 7:23am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233 "2013-01-09T07:23:49Z")\
**Posts on this page:** 7\
**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:** [January 9, 2013, 7:23am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/1 "2013-01-09T07:23:49Z")

</div>

**[magnuskriel](https://sourceforge.net/u/magnuskriel/)** wrote on [Wednesday, January 09, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#fa2b):

If I have a task with local variables, these variables are also stored on the task’s stack right?  
If I call a function in this task( function\_A ), which has local non-static variables, will these variables also be allocated from the task’s stack? And what if function\_A calls function\_B which has also non-static variables?

Thanks.

---

<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:** [January 9, 2013, 9:53am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/2 "2013-01-09T09:53:08Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, January 09, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#60c9):

Tasks are just C functions.&nbsp; The compiler knows nothing about the kernel or how the tasks will run and allocates non static local variables to either the task stack or registers (depending on the compiler and optimisation level) just as if the functions were called from main() before the scheduler was started.

Remember that each task has its own stack, so the variables that are on the stack are only seem by that task.

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 9, 2013, 11:49am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/3 "2013-01-09T11:49:44Z")

</div>

**[magnuskriel](https://sourceforge.net/u/magnuskriel/)** wrote on [Wednesday, January 09, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#d9bb):

Ok,

Just to get more clarity:

Does the local non static memory form part of the task context?  
portRESTORE\_CONTEXT() and portSAVE\_CONTEXT() only pops and pushes the processor registers. The local variables are preserved in the task’s stack when the task is switched out by another task?  
Any other functions which are called within the task is handled as per normal(kernel has no influance on the local variables of those functions ;&nbsp; these variables are located on the main heap)?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 9, 2013, 11:53am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/4 "2013-01-09T11:53:45Z")

</div>

**[magnuskriel](https://sourceforge.net/u/magnuskriel/)** wrote on [Wednesday, January 09, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#5936):

Sorry, made a typo in my previous reply(see bold text below):

Does the local non static **_variables_** form part of the task context? portRESTORE\_CONTEXT() and portSAVE\_CONTEXT() only pops and pushes the processor registers. The local variables are preserved in the task’s stack when the task is switched out by another task?  
Any other functions which are called within the task is handled as per normal(kernel has no influance on the local variables of those functions ; these variables are located on the main heap)?

---

<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:** [January 9, 2013, 12:04pm UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/5 "2013-01-09T12:04:12Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, January 09, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#5555):

The stack variable are indirectly part of the the task context because the stack pointer is one of the registers saved by portSAVE\_CONTEXT() and restored by portRESTORE\_CONTEXT().

If a task places variables (local non static variables) on the stack, gets switched out, then gets switched back in again, its execution context will not have changed - everything will be exactly as it was when it was switched out - including the variables on its stack.&nbsp; If another task runs the stack pointer will have been moved to point to the stack (and variables on the stack) that is unique to that task.&nbsp;

> these variables are located on the main heap

Only memory allocated by malloc()&nbsp; uses the heap - whether you have an RTOS or not.

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:** [January 10, 2013, 4:31am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/6 "2013-01-10T04:31:40Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, January 10, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#656b):

One other thing to note, that co-routines are a different beast, as co-routines share the stack, and have a bunch of special rules on how to implement themselves (and what context get saved between switches).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 10, 2013, 5:56am UTC](https://forums.freertos.org/t/nested-function-calls-and-memory-usage/3233/7 "2013-01-10T05:56:59Z")

</div>

**[magnuskriel](https://sourceforge.net/u/magnuskriel/)** wrote on [Thursday, January 10, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/8f15de2a/#ee38):

Thanks for all the info. I have some clarity now.
