Do the C++ operators new and new[] throw std::bad

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

I just recently learned that exceptions are supported in Android.


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.


I didn't think exceptions were supported on Android. If this has been changed recently could you post the link to your reference article? I was under the impression a failed allocation with new operator will return a null pointer on Android.

In which case if you use (nothrow) on your end you should comply with their default allocation behavior and get the same result as expected on Android.

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

链接地址: http://www.djcxy.com/p/54118.html

上一篇: 在Visual Studio C#Express 2010中调试Nunit测试

下一篇: C ++运算符是新的还是新的[]抛出std :: bad