# Using pvPortMalloc() in application 

**URL:** <https://forums.freertos.org/t/using-pvportmalloc-in-application/6874>\
**Category:** Kernel\
**Created:** [January 1, 2018, 6:50am UTC](https://forums.freertos.org/t/using-pvportmalloc-in-application/6874 "2018-01-01T06:50:03Z")\
**Posts on this page:** 3\
**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 1, 2018, 6:50am UTC](https://forums.freertos.org/t/using-pvportmalloc-in-application/6874/1 "2018-01-01T06:50:03Z")

</div>

**[gauravpatni](https://sourceforge.net/u/gauravpatni/)** wrote on [Monday, January 01, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/4ddff858/#8db4):

Hi,

I want to use pvPortMalloc() in application code ( STM32L4 , cortex M4) to create temp large size ( 1k to 30K , MCU has 128K RAM ) buffer .  
I reffered similar requirement post [https://www.freertos.org/FreeRTOS\_Support\_Forum\_Archive/November\_2013/freertos\_Direct\_using\_pvPortMalloc\_and\_vPortFree\_032bc6bfj.html](https://www.freertos.org/FreeRTOS_Support_Forum_Archive/November_2013/freertos_Direct_using_pvPortMalloc_and_vPortFree_032bc6bfj.html)

from that i came to know that application can use pvPortMalloc() as it is thread safe & suggest to use heap\_4.c.

1. But my doubt is thet heap\_4.c dosent efficiently uses memory as pvPortMalloc() uses portBYTE\_ALIGNMENT for BlockLink\_t struct & start of meory buffer . due to that every time few bytes are wated ( worst case is 7 per alignment ) .  
This may lead to unnecessary wating of RAM if no of pvPortMalloc() calls are more .

2. also while portBYTE\_ALIGNMENT why portBYTE\_ALIGNMENT == 8 ? why it cant be 4 , 2 or 1?

Regards,  
Gaurav

---

<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 1, 2018, 1:21pm UTC](https://forums.freertos.org/t/using-pvportmalloc-in-application/6874/2 "2018-01-01T13:21:35Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Monday, January 01, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/4ddff858/#f42c):

All dynamic memory allocations will have a similar possible loss of memory due to alignment issues. The value of 8 for the alignment is based on the rule that ARM processors have some data that needs to be aligned on 8 byte boundries, so dynamic memory allocations make sure that all allocations are on such a boundry because they can’t know if it is needed for this block or not.

Note, even if you statially allocate you memory, you will still run in to some memory loss from alignement, it can be less because the compiler know the needed alignment for each variable, but it still occurs.

---

<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 1, 2018, 11:49pm UTC](https://forums.freertos.org/t/using-pvportmalloc-in-application/6874/3 "2018-01-01T23:49:27Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, January 01, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/4ddff858/#f42c/b061):

To add to Richard D’s post - it is actually a requirement of the C  
standard to return an aligned address and the alignment requirement  
comes from the processor not the RTOS.
