8 years, 10 months 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:

        windowBuf = new unsigned char [windowSize];
        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.]

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 2014

Hi Erik, We're chasing either other about the site today...

Here was my test, which just eats memory.

#include "mbed.h"   // blinky starts with mbed lib 76, same results with lib 83

Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);

int main() {
    pc.baud(460800);    // I like a snappy terminal, so crank it up!
    pc.printf("\r\nBlinky eating memory - Build " __DATE__ " " __TIME__ "\r\n");
    int i = 2000;
    int total = 0;
    while(1) {
        myled = 1;
        pc.printf("Allocating %d bytes in addition to %d already allocated.\r\n", i, total);

        // ====================================================
        //char * p = new char [i];      // This never returns after 24000
        char * p = (char *)malloc(i);     // This fails gracefully with NULL return after 24000
        // ====================================================

        if (p) {
            total += i;
            pc.printf("  Success! \r\n");
        } else {
            pc.printf("  Failed...\r\n");
        myled = 0;

And then I went reading - c++ new was one site of interest.

posted by David Smart 22 Mar 2014

So 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 2014

I think I found it on the arm compiler site.

operator new throws exception.

posted by David Smart 23 Mar 2014

I 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

1 Answer

5 years, 7 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