# printf and heap\_4

**URL:** <https://forums.freertos.org/t/printf-and-heap-4/6567>\
**Category:** Kernel\
**Created:** [May 22, 2017, 3:19pm UTC](https://forums.freertos.org/t/printf-and-heap-4/6567 "2017-05-22T15:19:58Z")\
**Posts on this page:** 10\
**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:** [May 22, 2017, 3:19pm UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/1 "2017-05-22T15:19:58Z")

</div>

**[mastupristi](https://sourceforge.net/u/mastupristi/)** wrote on [Monday, May 22, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#1195):

I use Freertos 9.0.0 with heap\_4, and I use printf function provided by newlib\_nano bunbled in [GNU ARM Embedded Toolchain 5-2016-q3-update](https://developer.arm.com/-/media/Files/downloads/gnu-rm/5_4-2016q3/gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2).  
My MCU is STM32F410RB.

I note that when printf is called for the first time, \_malloc\_r() function is called (here the call stack):  
 ![](https://s18.postimg.org/4wbt6g089/sbk.png)

In this scenario I have two heap managment: one from heap\_4 and another from newlib\_nano. For the last one I don’t have a direct control.  
I wonder what is the right thing to do: make the newlib\_nano to call pvPortMalloc and vPortFree (Similar to what is described [here](https://sourceforge.net/p/freertos/discussion/382005/thread/0ff8e3f7/#9597)), or I have to use heap\_3 memory manager

best regards  
Max

---

<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:** [May 22, 2017, 3:40pm UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/2 "2017-05-22T15:40:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, May 22, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#1195/59a9):

Personally I would direct newlib memory allocation calls to the FreeRTOS  
equivalents. You have to be careful with functions like realloc though,  
for which there is no FreeRTOS equivalent. I normally define such  
functions in main.c and put an assert() in them to see if they ever get  
called.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 22, 2017, 3:55pm UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/3 "2017-05-22T15:55:02Z")

</div>

**[mastupristi](https://sourceforge.net/u/mastupristi/)** wrote on [Monday, May 22, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#1195/59a9/b824):

unfortunately it doesn’t calls malloc. It calls \_malloc\_r() that is not a weak function.  
I don’t know what are the differencies between \_malloc\_r() and malloc() (and so pvPortMalloc()).  
Another point is that other \*malloc\* functions could be called

best regards  
Max

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 22, 2017, 4:00pm UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/4 "2017-05-22T16:00:51Z")

</div>

**[mastupristi](https://sourceforge.net/u/mastupristi/)** wrote on [Monday, May 22, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#1195/59a9/058d):

I tried to link with this option:

```auto
--defsym=_malloc_r=pvPortMalloc

```

but I have this error:

```auto
gcc-arm-none-eabi-5_4-2016q3-20160926/src/newlib/newlib/libc/stdlib/nano-mallocr.c:239: multiple definition of `_malloc_r'

```

the same if I define \_malloc\_r() in my main o if I alias it

best regards  
Max

---

<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:** [May 22, 2017, 4:11pm UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/5 "2017-05-22T16:11:05Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, May 22, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#1195/59a9/058d/eff1):

I think C should only attempt to pull undefined symbols from the library

- that is - the symbol is not in the application so look for it in the  
library. However GCC seems to want to complain when it seems the same  
symbol defined in both the application and the library unless you  
explicitly tell it not to, which you have to do using a command line  
option. Search the GCC manual for something like ‘multiply defined’ to  
find the necessary option.

---

<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 23, 2017, 1:01am UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/6 "2017-05-23T01:01:02Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Tuesday, May 23, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#9afa):

If your implementation uses newlib-nano, another option is to look to see if they are using the RTOS compatible versions which call a \_\_malloc\_lock() and \_\_malloc\_unlock(), which you can then define to call the FreeRTOS scheduler stop/restart functions like heap3.h does. Then malloc and family are as threadsafe as pvPortMalloc().

---

<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 23, 2017, 5:58am UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/7 "2017-05-23T05:58:13Z")

</div>

**[hs2sf](https://sourceforge.net/u/hs2sf/)** wrote on [Tuesday, May 23, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#f389):

You could also use the ‘–wrap’ linker flag and provide the corresponding wrapper functions.

---

<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 23, 2017, 6:43am UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/8 "2017-05-23T06:43:56Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, May 23, 2017](https://sourceforge.net/p/freertos/discussion/382005/thread/f2b0ee0c/#de5c):

Hi Massimiliano,

Like Richard, I also tend to avoid the clib or newlib heap functions ( and their reentrant equivalents `_r` ), by using the linker or a `configASSERT()`.

The FreeRTOS+ library has its own task-safe implementation of all `printf()` functions, which:

● Does not use the HEAP  
● Has a low usage of the stack  
● May be called from within an ISR as well

The module `printf-stdarg.c` needs two external functions, provided by you:

```auto
    /*
     * 'aAddress' is a memory address.
     * Return 1 for readable, 2 for writeable, 3 for both.
     * Function must be provided by the application.
     */
    extern BaseType_t xApplicationMemoryPermissions( uint32_t aAddress );
    
    /*
     * vOutputChar() shall output a character to "stdout"
     * It is called in case a sprintf() function is called with
     * the NULL pointer.
     */
    extern void vOutputChar( const char cChar, const TickType_t xTicksToWait );

```

Note that this crash will be avoided if you implement `xApplicationMemoryPermissions()` :

```auto
    int iNumber = 12;
    /* In the printf call %s is used in stead of %d,
    which may lead to a crash. The printf functions
    in printf-stdarg.c will print it as "INV_MEM" */
    printf( "The answer is %s\n", iNumber );

```

In case you do need a `realloc()` function, I attached a tested version in which it is called:

```auto
    void *pvPortRealloc( void *pvAddres, size_t uxNewSize );

```

It adds a new function `uxPortGetSize()` which returns the length of any pointer in the heap ( using heap\_4 or heap\_5 ). The length is rounded up to a multiple of e.g. 8 bytes, due to its allocation.

See attachment `realloc_and_printf.zip` for both source files.

Regards.

---

<div class="post-metadata">

**Author:** ![syduan](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@syduan](https://forums.freertos.org/u/syduan)\
**Post date:** [February 14, 2020, 9:22am UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/9 "2020-02-14T09:22:54Z")

</div>

hi Hein, I ran into the compile error with using the printf-stdarg.c you provied due to the lack of implementation of both external functions. I cound not find the realloc\_and\_printf.zip file you mentioned above which would likely be helpful to resolve this compile error.

thanks  
-Allen

---

<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:** [February 14, 2020, 9:37am UTC](https://forums.freertos.org/t/printf-and-heap-4/6567/10 "2020-02-14T09:37:40Z")

</div>

There you are:

[printf-stdarg\_2020\_02\_14.zip](https://forums.freertos.org/uploads/short-url/tPkgQONtCNyOIAonfT1urjT1Hgq.zip) (4.6 KB)

Note that it can not print floating point numbers.  
It **can** be used ( if necessary ) in ISR code, and it has a very low usage of the stack.  
It does not use the heap at all.  
If you want that string pointers are checked for validity, make sure you define `xApplicationMemoryPermissions()`. Let it return 1 for readable, 2 for writable, 3 when both are allowed.
