# Heap\_5 with stdlib

**URL:** <https://forums.freertos.org/t/heap-5-with-stdlib/9211>\
**Category:** Kernel\
**Created:** [March 11, 2020, 5:28am UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211 "2020-03-11T05:28:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin152](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@martin152](https://forums.freertos.org/u/martin152)\
**Post date:** [March 11, 2020, 5:28am UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/1 "2020-03-11T05:28:41Z")

</div>

Dear,  
I implemented heap\_5 and it works perfectly, but for some stdlib functions that I use as strtok, they use malloc\_r being out of the context of heap\_5 and generating error.  
My question is how Freertos addresses this situation.  
The development environment used is KDS 3.2 with a K64F processor.

---

<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:** [March 11, 2020, 5:46am UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/2 "2020-03-11T05:46:06Z")

</div>

Aren’t `strtok()` and `malloc_r` still using the original `malloc()`? Or did you redefine `malloc()` to use heap\_5?

---

<div class="post-metadata">

**Author:** ![martin152](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@martin152](https://forums.freertos.org/u/martin152)\
**Post date:** [March 11, 2020, 10:04am UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/3 "2020-03-11T10:04:52Z")

</div>

I have made #define malloc (size) pvPortMalloc (size), but this applies to the malloc that I write, I don’t know how to make stdlib also comply with this directive.

---

<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 11, 2020, 10:44am UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/4 "2020-03-11T10:44:03Z")

</div>

Unless you recompile the library with your define in it, it won’t change what the library calls. If your library is newlib, and the vendor compiled it right, then there is a way to make malloc thread safe by defining functions \_malloc\_lock and \_malloc\_unlock to protect the code within malloc.

Note also that some functions, like strtok are not thread safe, so you need to use them with care, or use the thread safe equivalents of them.

---

<div class="post-metadata">

**Author:** ![martin152](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@martin152](https://forums.freertos.org/u/martin152)\
**Post date:** [March 11, 2020, 1:06pm UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/5 "2020-03-11T13:06:22Z")

</div>

> [@martin152](#):
>
> pvPortMalloc

I see that being stdlib a compiled library will never be able to access pvPortMalloc from heap\_5, it seems to me that one way would be for stdlib’s required functions that use dynamic allocation to be rewritten within my application, which would ultimately only be strtok.  
I appreciate the answers.

---

<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:** [March 11, 2020, 2:44pm UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/6 "2020-03-11T14:44:29Z")

</div>

In my experience, you can redefine any function from the C standard library.  
When you define your version of `malloc()`, it will be called from both `strtok()` or any other clib function.

---

<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, 2020, 2:41am UTC](https://forums.freertos.org/t/heap-5-with-stdlib/9211/7 "2020-03-12T02:41:54Z")

</div>

The biggest issue with defining your own version of standard library functions (besides the fact that technically, it produces undefined behavior) is that you need to know a lot about the implementation to get it right. There may be related functions that have internal dependancies that need to be handled, and you can get functions that you think would be using that function, actually using an internal version instead. The OP commented about stroke calling malloc\_r, which is newlib’s internal reentrant version of malloc.
