heinbali01 wrote on Tuesday, May 23, 2017:
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:
/*
* '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()
:
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:
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.