2019-04-10 11:25 AM
2019-04-10 11:30 AM
Speaking only to what I know, malloc/new/free dynamic heap allocation is to be avoided in embedded space due to the SRAM that always need to be optimized. "unions" with static memory allocation or temporary storate in the stack would be prefferable when possible.
2019-04-10 11:46 AM
In Keil perhaps implement the hosting on your target properly so it outputs whatever message it is trying to output to the terminal.
There aren't infinite resources in embedded, perhaps there is a throw/catch mechanism to handle this, or some kind of structure exception handler.
//****************************************************************************
// Hosting of stdio functionality through USART3
//****************************************************************************
/* Implementation of putchar (also used by printf function to output data) */
int SendChar(int ch) /* Write character to Serial Port */
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART3, ch);
return(ch);
}
//****************************************************************************
#include <rt_misc.h>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) { return (SendChar(ch)); }
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch) { SendChar(ch); }
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
//****************************************************************************
2019-04-10 12:16 PM
You are right - it is trying to write an error message to an unimplemented character routine. I put in my own _ttywrch and I see the message "SIGABRT: Abnormal termination".
Presumably there is some kind of exception handler being triggered - any idea how I can defeat it ? I'll look at the rt_misc error handling functions.
2019-04-11 08:12 AM
Unlike malloc(), new is never supposed to return zero/NULL/nullptr. It either returns the requested item, fully constructed or it throws an exception. UNLESS you use the "no throw" syntax for new :)
2019-04-11 09:50 AM
You are of course correct - I was fooled because other compilers I use do not do this, presumably because they are non-compliant..
The Keil/Arm compiler that St supplies (V5.06) does not recognize (std::nothrow), but buried deep in the manual I found "--force_new_nothrow".
This will probably solve the problem, otherwise I shall just have to write better code.
To me as an embedded guy, it seems that the WORST thing any code could do is to exit the application, as this is by definition the end of the world, whereas for a PC application it might be quite reasonable. Ah well.
2019-04-11 09:59 AM
Or add a try/catch block around the new call. The C++ paradigm is that uncaught exceptions halt the program. So if you don't want that to happen make sure to catch them. Or enable the nullptr return and check for that. It all depends on how "C"-like you want your C++ code to look :)