Do C ++ statements make new and new [] throw std :: bad_alloc on Android?

Is any exception thrown when an unsuccessful attempt to allocate memory is thrown?

I recently found out that exceptions are supported on Android.

+8
source share
2 answers

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

I. Support for C ++ Exceptions:

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

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

LOCAL_CPPFLAGS += -fexceptions 

Simply put, add one line to your Application.mk, the configuration will be automatically applied to all your NDK project modules:

 APP_CPPFLAGS += -fexceptions 

NOTE. The legacy "arm-eabi-4.4.0" toolchain provided for backward compatibility with this NDK does not support exceptions!

Thus, exceptions appear to be supported if the application is compiled with '-exceptions'. Therefore, I understand that code compiled with -fexceptions will call std :: bad_alloc when the memory allocation fails.

+4
source

I did not think that exceptions are supported on Android. If this has recently been changed, could you post a link to your article? It seemed to me that a failed selection with a new operator would return a null pointer to Android.

In this case, if you use (nothrow) at your end, you should follow their default behavior and get the same result as expected on Android.

http://www.cplusplus.com/reference/std/new/nothrow/

0
source

All Articles