# How does memory get allocated on the heap when using an static array?

**URL:** <https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614>\
**Category:** Kernel\
**Created:** [May 19, 2021, 9:47am UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614 "2021-05-19T09:47:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benj](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Benj](https://forums.freertos.org/u/Benj)\
**Post date:** [May 19, 2021, 9:47am UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/1 "2021-05-19T09:47:07Z")

</div>

How does memory get allocated on the heap when using a static array in heap\_1.c for example?

---

<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:** [May 19, 2021, 12:52pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/2 "2021-05-19T12:52:27Z")

</div>

I afraid I don’t fully understand your question.  
Do you have questions regarding the implementation details of heap\_1.c after reading the source ?

---

<div class="post-metadata">

**Author:** ![Benj](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Benj](https://forums.freertos.org/u/Benj)\
**Post date:** [May 19, 2021, 2:00pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/3 "2021-05-19T14:00:56Z")

</div>

Yes exactly, heap\_1.c for example uses a static array.  
Don’t fully understand how it manages the heap?

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [May 19, 2021, 2:25pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/4 "2021-05-19T14:25:03Z")

</div>

When you google for “freertos heap\_1”, you will find all information about the different heap algorithms, from heap\_1 to heap\_5.  
( Google brought me [here](https://www.freertos.org/a00111.html#heap_1) ).

---

<div class="post-metadata">

**Author:** ![Benj](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Benj](https://forums.freertos.org/u/Benj)\
**Post date:** [May 19, 2021, 2:30pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/5 "2021-05-19T14:30:34Z")

</div>

Ok, yes I’ve googled and read the FreeRTOS documentation, I found this:

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/7/7dc721314baef8add21e6003aea2feca8eac40e3.png)

So, heap\_1.c doesn’t really allocate memory on the heap

---

<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:** [May 19, 2021, 3:24pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/6 "2021-05-19T15:24:43Z")

</div>

Exactly

> <https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/ac2c383bc14b2577101cad238c8779f4d9c14d6c/portable/MemMang/heap_1.c#L54>

---

<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:** [May 19, 2021, 3:55pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/7 "2021-05-19T15:55:05Z")

</div>

> [@Benj](#):
>
> So, heap\_1.c doesn’t really allocate memory on the heap

It somewhat depends on what you want to call ‘the heap’. There is ‘the heap’ that C provides, which is only used in heap\_3.c, which provide wrappers for malloc and free so they can be used in a thread safe manner.

The other heap files CREATE there own equivalent of that heap for use by FreeRTOS and other parts of the program that use those same routines. Each version provide different capabilities, and all (except heap\_3.c) work out of static arrays for the allocations.

---

<div class="post-metadata">

**Author:** ![Benj](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Benj](https://forums.freertos.org/u/Benj)\
**Post date:** [May 20, 2021, 7:29am UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/8 "2021-05-20T07:29:28Z")

</div>

I mean the ‘heap’ defined within a specific memory region in RAM in my STM32 MCU.

> @richard-damon:  
> The other heap files CREATE there own equivalent of that heap for use by FreeRTOS and other parts of the program that use those same routines. Each version provide different capabilities, and all (except heap\_3.c) work out of static arrays for the allocations.

Ok I understand the conecpt. Thanks

---

<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:** [May 20, 2021, 11:00am UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/9 "2021-05-20T11:00:44Z")

</div>

Yes, the key is THAT heap, as defined by the link map, is only used by malloc and company. To use that, you need to use heap\_3.c (and watch out that malloc by itself may. not be thread-safe)

---

<div class="post-metadata">

**Author:** ![simonjwright](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/simonjwright/32/4770_2.png) [@simonjwright](https://forums.freertos.org/u/simonjwright)\
**Post date:** [May 23, 2021, 3:53pm UTC](https://forums.freertos.org/t/how-does-memory-get-allocated-on-the-heap-when-using-an-static-array/12614/10 "2021-05-23T15:53:31Z")

</div>

Cortex-M: I use heap\_5.c, and configure the whole of RAM from the end of BSS to \<some amount for interrupts, default 1024\> below the initial stack top. Since I’m writing in Ada, I don’t need to worry about malloc/free.

Hmm. I can see that will be an issue if I call some Newlib function that does use malloc/free.
