Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years ago.
Is it possible to enable exception handling?
Error: Support for exception handling is disabled; use - -exceptions to enable in "process.cpp", Line: 631, Col: 5
my construct was:
try { windowBuf = new unsigned char [windowSize]; } catch(int) { windowBuf = NULL; }
I ask because a relatively simple construct
windowBuf = new unsigned char [windowSize];
will "hang" if "windowSize" memory is not available. I'm no c++ expert, but I guess most c++ compilers throw an error rather than returning NULL. Attempts to trap that as shown above failed with the compiler error.
[And as a partial response to Erik's note below, I had first tried to treat it like c malloc, but that didn't work.]
1 Answer
6 years, 9 months ago.
Just to point out that new does not return NULL when it fails by default. It throws an exception
If you want it to return null you have to specify "nothrow" when using new
It should be returning NULL, which is the normal way to then see it couldn't allocate it. But I just also has someone else who had a program where is just hangs instead of returning NULL.
To be fair, generally if it runs out of memory in the end your program will crash anyway on something else which isn't handled when there is no free memory left, but it should return a NULL pointer.
posted by Erik - 22 Mar 2014Hi Erik, We're chasing either other about the site today...
Here was my test, which just eats memory.
And then I went reading - c++ new was one site of interest.
posted by David Smart 22 Mar 2014So from your code malloc works fine, but new fails to return a NULL pointer? That should be a bug.
And yeah, see you also found that post :P.
But afaik it really just should return null pointer (I wouldn't be surprised if using exceptions is more memory intensive, not great on an embedded device).
posted by Erik - 22 Mar 2014I think I found it on the arm compiler site.
operator new throws exception.
posted by David Smart 23 Mar 2014I found many other threads of discussion that were similar to this. What I found most useful is the reply from Damian Gower in Implement your own new - delete.
posted by David Smart 18 Jun 2014