做 C++运算符 new 和 new[] 在 Android 上抛出 std::bad_alloc?
当尝试分配内存失败时会抛出异常吗?
我最近才知道 Android 支持异常。
Will any exception be thrown when an attempt to allocate memory fails?
I just recently learned that exceptions are supported in Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我下载了 ndk 并在文档文件夹 CPLUSPLUS-SUPPORT.HTML 中找到了它。
因此,只要应用程序使用“-fexceptions”编译,异常似乎就受到支持。所以我的理解是,使用 -fexceptions 编译的代码将在分配内存失败时抛出 std::bad_alloc 。
I downloaded the ndk and found this in the docs folder, CPLUSPLUS-SUPPORT.HTML .
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.
我认为 Android 不支持异常。如果最近发生了变化,您可以发布参考文章的链接吗?我的印象是,在 Android 上使用 new 运算符分配失败将返回空指针。
在这种情况下,如果您最终使用
(nothrow)
,您应该遵守其默认分配行为,并获得与 Android 上预期相同的结果。http://www.cplusplus.com/reference/std/new/nothro/
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/