- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 new 运算符中使用 C++ 常量值 std::nothrow 以避免失败时出现异常,而是返回一个空值。但是正如我所尝试的那样,这似乎不适用于我的环境,即 Linux x86_64 上的 g++ 4.4.4。
以下为测试程序及执行结果:
#include <stdlib.h>
#include <new>
#include <iostream>
class TT {
public:
TT(int size);
~TT();
private:
char * buffer;
};
TT::TT(int size) : buffer(NULL) {
if (size <= 0) {
throw std::bad_alloc();
}
buffer = (char *)malloc(size);
if (buffer == NULL) {
throw std::bad_alloc();
}
}
TT::~TT() {
if (buffer != NULL) {
delete buffer;
}
}
int main(int argc, char * argv[]) {
TT * tt = NULL;
try {
tt = new TT(0);
} catch (std::bad_alloc& ba) {
std::cout << "tt exception caught" << std::endl;
}
tt = new (std::nothrow) TT(0);
if (tt == NULL) {
std::cout << "tt is null" << std::endl;
}
else {
std::cout << "tt is not null" << std::endl;
}
return 0;
}
执行结果:
$ uname -i
x86_64
$ g++ --version
g++ (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ t.cpp
$ ./a.out
tt exception caught
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
从输出消息来看,抛出了一个异常;但我希望它不应该,而是期望返回空指针。谁能帮我解决这个问题。谢谢。
最佳答案
当然有一个 std::bad_alloc
抛出。 您自己的代码会抛出它。
new (std::nothrow)
仅指定 new 表达式的内存分配器不会抛出。但是一旦您的 TT
对象在该内存中被构建,它仍然可能抛出它喜欢的任何异常。
关于c++ - Linux g++ new (std::nothrow) 确实有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46925296/
我要选择column1小于或等于 column2 的数据.但是,当我尝试使用 { console.log(notify) res.render('index', { title
我在页面中有一个更新面板,供用户指定事件的“所有者”。该所有者将是我们事件目录中的用户。该面板允许用户输入一些文本,然后单击搜索按钮以在我们的 Active Directory 中查找匹配的用户。当用
我想知道为什么同一个实体在控制台中调用的浏览器 window 对象中有不同的表示: 如果我们仅在浏览器控制台内调用window,我们将获得完整的浏览器信息。其中文档将表示为类似对象的实体,包括各种 P
我有一个正在处理大量数据的应用程序,我正在监视它的 .NET 内存性能计数器。基于性能计数器,所有堆中的#Bytes 正在缓慢增长(每 12 小时约 20MB)。所有 3 代也被收集(gen0 每秒几
According to documentation 默认实现不执行任何操作。 但是...我从 drawRect 方法中抛出异常并且我看到了下一个调用堆栈 3 EasyWakeup
我使用 package maven 插件构建了一个 Spring 应用程序的 jar。在本地,我可以运行它java -jar -Dspring.profiles.active=dev target/m
我们正在开发一种软件,我们可以在其中预览和记录从视频采集卡采集的输入视频源。预览是用DirectShow实现的,录制是用Media Foundation实现的(老软件慢慢升级到MediaFound
我在 symfony 中构建了一个简单的操作,它通过 wkhtmltopdf 生成一个 PDF 文件并将其输出到浏览器。 代码如下: $response = $this->getResponse(
这将是一个简短的问题。 来源在这里:https://github.com/exhuma/grc 标题解释了我的问题;) 我注册并上传了包到pypi。现在,如果我运行 easy_install grc,
我需要将事务与LINQ to SQL一起使用,并且正在阅读以熟悉它。 确实已处理SubmitChanges吗? 最佳答案 当您调用SubmitChanges时,LINQ to SQL将检查一组已知对象
在 Ajax 结果之后,我试图淡出 html 按钮。这在大多数情况下都有效,但有时按钮不会淡出,我不明白为什么。 $.ajax({ type: frm.attr('method'),
我以这样的方式创建 Cesium.Primitive: _createPrimitive: function (linesNumber) { var instances = [
我正在 Spring 上构建一个 Web 应用程序。 我很困惑是否应该在配置中使用 mvc:annotation-driven 还是 context:annotation-config 来扫描注释来处
问题 - 给定一个字符串 '0'、'1' 和 '?'。生成所有可能的字符串,您可以在其中替换“?”用“0”或“1”? 例如 - 输入 - “0??” 输出 - “000”、“001”、“010”、“0
总结: 修改wxwidgets Hello World tutorial中的代码后在 CppMicroServices 框架中的“模块”中,使用事件表或 Bind() 注册的事件似乎不会触发,但使用
我的 Capistrano 任务失败了 No such file or directory @ rb_sysopen - /home/blog/pids/grantb.blog.staging.pid
我们在 repo 协议(protocol)中有一个配置文件,所有用户都根据他们的功能做出贡献。对于我的本地测试,我需要手动更改该配置文件中的两个值,但我不想将我的更改提交回服务器。 但是,如果存储库中
我在顶部菜单(ListItems)的开头插入一个菜单选项并使用 $('#newMenuItem').show('slide'); 成功了。但是,我不喜欢它从左上角滑入的方式,所以我将其更改为 $('#
我尝试在我的 NSWindow 上调用 setFrame:display:animate: 并且没有执行任何操作,但是如果我在同一个窗口上调用 setFrame:display:,它有效。 我需要做其
我是一名优秀的程序员,十分优秀!