# Heap Memory and large Array allocation

**URL:** <https://forums.freertos.org/t/heap-memory-and-large-array-allocation/10106>\
**Category:** Kernel\
**Created:** [June 24, 2020, 8:22am UTC](https://forums.freertos.org/t/heap-memory-and-large-array-allocation/10106 "2020-06-24T08:22:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FlavioB](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@FlavioB](https://forums.freertos.org/u/FlavioB)\
**Post date:** [June 24, 2020, 8:22am UTC](https://forums.freertos.org/t/heap-memory-and-large-array-allocation/10106/1 "2020-06-24T08:22:29Z")

</div>

Hi guys,

I’m sure that is not a valid concern, but I prefer to have an answer by those who have more experience than me.

I’m using FreeRTOS 9.0 and heap\_4.c for heap memory management.

In order to simplify my question, let us imagine that we use a 8-bit processor (so, a word is a byte)

Now, I need to create a task after creating and deleting another tasks (so, my heap memory can be a little bit fragmented):

> ReturnState = xTaskCreate( MyTask,  
> ( char \* ) “MyTask”,  
> 4 \* configMINIMAL\_STACK\_SIZE,  
> ( void \* ) NULL,  
> 1,  
> NULL);

Of course, the configTOTAL\_HEAP\_SIZE is large enough for another task.

MyTask code is the following:

> ```
> void MyTask( void *pvParameters )
> {
> uint8_t MyArray[3*configMINIMAL_STACK_SIZE];
> 
> memset((void *)&MyArray[0],0xA5,sizeof(MyArray));
> 
> printf("The last number of my array is: %d",MyArray[3*configMINIMAL_STACK_SIZE-1]);
> 
> for(;;;) __asm('NOP');
> }
> 
> ```

In MyTask I’ve created an array with size larger than configMINIMAL\_STACK\_SIZE. If heap memory is fragmented, FreeRTOS ensure me that the MyArray is allocated to a single memory block ?

Thanks for your reply.

Sincerely,

FlavioB

---

<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:** [June 24, 2020, 10:18am UTC](https://forums.freertos.org/t/heap-memory-and-large-array-allocation/10106/2 "2020-06-24T10:18:14Z")

</div>

FreeRTOS is not responsible the binary layout of `MyArray` it‘s just the C/C++ standard.  
The same would apply to allocating `MyArray` from heap provided the heap implementation is standard compliant.  
In your case yes, `MyArray` is contiguous, which can be verified with  
`sizeof(MyArray) == sizeof(MyArray[0]) * 3*configMINIMAL_STACK_SIZE`  
because there is no alignment constraint with `uint8_t` elements.

BTW I’m pretty sure that your code will fail with stack overflow because of using pretty stack hungry `printf`.

---

<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:** [June 24, 2020, 10:40am UTC](https://forums.freertos.org/t/heap-memory-and-large-array-allocation/10106/3 "2020-06-24T10:40:56Z")

</div>

The FreeRTOS memory allocation routines can only return the requested memory as a contiguous block, or they will fail and return NULL. If the heap gets too fragmented you get allocation failures, not some strange way to return a disjointed block of memory.

---

<div class="post-metadata">

**Author:** ![FlavioB](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@FlavioB](https://forums.freertos.org/u/FlavioB)\
**Post date:** [June 24, 2020, 12:30pm UTC](https://forums.freertos.org/t/heap-memory-and-large-array-allocation/10106/4 "2020-06-24T12:30:54Z")

</div>

Dear Richard and Hartmut.

Thanks for your reply, you have confirmed to me the behavior that I expected.

Sincerely,

FlavioB
