Why malloc and free can cause problem in freeRTOS?

bou6 wrote on Saturday, August 27, 2016:

I read that freeRTOS provide the two functions vPortFree and vPortmalloc as an alternative to malloc and free of glibc, however according to this reference http://stackoverflow.com/questions/855763/is-malloc-thread-safe
malloc and free are thread safe.
So my question is why freeRTOS provide this alternative and what can happen if i call malloc and free

richard_damon wrote on Saturday, August 27, 2016:

Note, that answer says it is thread safe when you compile with -pthreads. That is an option used for POSIX systems, i.e with full blown operating systems.

Some embeded libraries have hooks that let you make them thread safe, but typically you need to implement those for your RTOS. I beleive FreeRTOS has available hooks to work with newlib, which is a common library for some enviroments. Basic FreeRTOS can’t assume that this is in use, as not systems use this. (There could be a variant of heap3 that does assume newlib and the RTOS hooks are being used that just calls malloc and free).

richard_damon wrote on Saturday, August 27, 2016:

The other thing, you need to be a bit careful about using C/C++ answers on a forum like that, as many people will just assume they are talking about a full hosted enviroment (with a full operating system like Windows or Linux or the like) and that may not be always applicable to small systems.

rtel wrote on Sunday, August 28, 2016:

See http://www.freertos.org/a00111.html In my opinion it is very rare that malloc() and free() are suitable for very small real time systems, unless they are provided by a specialist company, such as IAR (and even then perhaps not, depending on what your particular application is actually needing).