According to the C++ standard, an exception has to be thrown
if new cannot allocate memory. Therefore
int* p = new int;
delete p;
int n = 1;
int rep = 1;
Serial.println("\nStart N_Exception_Basics::test_too_much_news(mit_Exceptions) ");
#ifdef __cpp_exceptions
try
{
while (p!=nullptr)
{
p = new int[n];
delete[] p;
n = n * 10;
rep++;
Serial.printf("n=%d p=%d \n", n,(int)p);
}
}
catch (std::exception& e)
{
Serial.printf("Exception bei test_too_much_news n=%d what()=%s\n", n, e.what());
}
catch (...)
{
Serial.printf("... Exception bei test_too_much_news \n");
}
Serial.printf("nach catch ... Exception bei test_too_much_news \n");
should never return p==0, but throw a badalloc exception if memory could not be allocated.
However, for an Raspberry Pico 2 and an STM32 I get results like
Start N_Exception_Basics::test_too_much_news(mit_Exceptions)
n=10 p=536892088
n=100 p=536894464
n=1000 p=536894464
n=10000 p=536894464
n=100000 p=536894464
n=1000000 p=536894464
n=10000000 p=0
n=100000000 p=0
n=1000000000 p=0
Exception bei test_too_much_news n=1000000000 what()=std::bad_array_new_length
nach catch ... Exception bei test_too_much_news
where p=0 results should not be.
For an ESP32 board I get no such p=0 results.
Do I need special compiler flags so that a I get a badalloc exception? Or is there something wrong with the compiler?
Thanks
Richard