Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do the C++ operators new and new[] throw std::bad_alloc on Android?

Will any exception be thrown when an attempt to allocate memory fails?

I just recently learned that exceptions are supported in Android.

like image 567
Anton Avatar asked Aug 04 '11 18:08

Anton


1 Answers

I downloaded the ndk and found this in the docs folder, CPLUSPLUS-SUPPORT.HTML .

I. C++ Exceptions support:

The NDK toolchain supports C++ exceptions, since NDK r5, however all C++ sources are compiled with -fno-exceptions support by default, for compatibility reasons with previous releases.

To enable it, use the '-fexceptions' C++ compiler flag. This can be done by adding the following to every module definition in your Android.mk:

LOCAL_CPPFLAGS += -fexceptions

More simply, add a single line to your Application.mk, the setting will automatically apply to all your project's NDK modules:

APP_CPPFLAGS += -fexceptions

NOTE: The obsolete "arm-eabi-4.4.0" toolchain provided for backwards compatibility with this NDK does not support exceptions!

So exceptions seems to be supported, as long as the application is compiled with '-fexceptions'. So my understanding is that code compiled with -fexceptions will throw std::bad_alloc on failure to allocate memory.

like image 70
Anton Avatar answered Sep 17 '22 23:09

Anton