- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 C++ 程序,其中重载了 new
运算符。问题是如果我在 new
运算符中的分配失败,我仍然会调用构造函数。我知道我可以通过抛出 std::bad_alloc
来避免这种情况,但我不想那样做。
我怎样才能在重载的 new
运算符中失败并且仍然不调用我的构造函数?本质上,我想实现类似 new (std::nothrow)
的东西。
这里有一个例子来说明我的意思。 请注意我正在测试的系统on 没有内存保护。所以访问 NULL
不会做任何事情
示例 1:重载新运算符
#include <stdio.h>
#include <stdlib.h>
#include <memory>
class Test {
public:
Test(void) {
printf("Test constructor\n");
}
void print(void) {
printf("this: %p\n", this);
}
void* operator new(size_t size, unsigned int extra) {
void* ptr = malloc(size + extra);
ptr = NULL; // For testing purposes
if (ptr == NULL) {
// ?
}
return ptr;
}
};
int main(void) {
Test* t = new (1) Test;
t->print();
printf("t: %p\n", t);
return 0;
}
这个的输出是:
$ ./a.out
Test constructor
this: 00000000
t: 00000000
很明显,构造器在 new
失败时被调用。
示例 2:使用 new (std::nothrow) 的大型类声明
#include <stdio.h>
#include <stdlib.h>
#include <memory>
class Test {
int x0[0x0fffffff];
int x1[0x0fffffff];
int x2[0x0fffffff];
int x3[0x0fffffff];
int x4[0x0fffffff];
int x5[0x0fffffff];
int x6[0x0fffffff];
int x7[0x0fffffff];
int x8[0x0fffffff];
int x9[0x0fffffff];
int xa[0x0fffffff];
int xb[0x0fffffff];
int xc[0x0fffffff];
int xd[0x0fffffff];
int xe[0x0fffffff];
int xf[0x0fffffff];
public:
Test(void) {
printf("Test constructor\n");
}
void print(void) {
printf("this: %p\n", this);
}
};
int main(void) {
Test* t = new (std::nothrow) Test;
t->print();
printf("t: %p\n", t);
return 0;
}
这个的输出是:
this: 00000000
t: 00000000
很明显,当 new
失败时,构造器不会被调用。
那么我如何在我的代码中实现new (std::nothrow)
类功能重载 new
运算符?
最佳答案
编译器是否在调用后检查空指针operator new
与否,在调用析构函数之前,取决于分配器函数是否有非抛出异常规范与否。如果不是,编译器假定如果没有可用内存,operator new
将抛出。否则,它假定 operator new
将返回一个空指针。在你的情况,你的 operator new
应该是:
void* operator new( size_t size, unsigned int extra ) throw()
{
//...
}
或者如果您可以依靠 C++11 支持:
void* operator new( size_t size, unsigned int extra) noexcept
{
}
关于c++ - `new (std::nothrow)`是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15291971/
使用 noexcept 等修饰符的目的是什么? , __declspec(nothrow) , throw() , __attribute__(nothrow)可能还有一些具有不同的语义、不同的编译器
以下代码在gcc-4.7.1下不能编译,而是在clang-3.2下编译。哪一个遵循 C++11 标准? struct X { virtual ~X() = default; }; struct Y
是否有可能在编译时或至少在使用 pc-lint 的静态分析期间强制使用 std::nothrow 严格使用运算符 new?使用 c++ (GCC) 4.8.3 20140911 (Red Hat 4.
为什么我不能让 operator++() 不抛出? 这可能是使用后缀++ 运算符(相对于前缀++ 运算符)的少数优势之一。 例如,这段代码不编译 class Number { public:
这个问题在这里已经有了答案: Will using new (std::nothrow) mask exceptions thrown from a constructor? (3 个答案) 关闭
我想在我的一些返回对象引用的成员函数上应用 __declspec(nothrow)。例如,我想将它添加到此函数(在 MyClass.h 中): CMyClass& operator= ( IN UIn
我试图指定一个函数是 nothrow每当 Foo 的析构函数时不扔。我可以通过使用类型特征 std::is_nothrow_destructible<> 来做到这一点.我怎样才能直接做到这一点?我试过
阅读前this question ,我从来没有认真对待过异常处理。现在我看到了必要性,但仍然觉得“编写异常安全代码非常困难”。 在该问题的已接受答案中查看此示例: void doSomething(T
在 Abrahams 创建的 Exception Safety 中,我们拥有三个保证:基本保证、强保证和不抛出保证。我可以说如果我有一个代码库使用 nothrow 作为"new"并且 noexcept
我有以下代码,但无法编译: int *p = new(nothrow) int; delete (nothrow) p; //Error 我得到的错误是: error C2440: 'delete'
std::nothrow的理想用法是什么? 最佳答案 我只会将它用作优化(或代码简化),否则我会在使用常规 new 时立即放置一个 try-catch block ,捕获 std::bad_alloc
在使用 CUDA 的内核调用中使用 new 时,检查内存分配是否成功的最佳方法是什么?如果没有办法继续执行内核,即使在内存分配失败的情况下,是否有类似于 (nothrow) 的东西? 谢谢! 最佳答案
我想写一个模板类MyClass接受 normal 和 noexcept 签名。例如MyClass和 MyClass . 这是我试过的: template struct IsNoThrow; templ
我有一个用例,我想使用 placement new 但也想确保 new 操作不会抛出。 我想我想要像下面这样的东西(注意这不能编译)但我不确定具体怎么做。 char buf1[100];
#include int main() { int *xx = new (std::nothrow) int[2]; if(xx == NULL) { exi
我有一个 C++ 程序,其中重载了 new 运算符。问题是如果我在 new 运算符中的分配失败,我仍然会调用构造函数。我知道我可以通过抛出 std::bad_alloc 来避免这种情况,但我不想那样做
如果一个类型有一个不会失败的交换函数,这可以让其他函数更容易提供 strong exception safety guarantee .这是因为我们可以首先完成所有可能会失败的函数工作,然后使用非抛出
在 C++ 的工作草案中阅读此内容。 T* p1 = new T; // throws bad_alloc if it fails T* p2 = new(nothrow) T; // returns
我正在查看一些 gcc 属性列表,我发现了这个引起了我的注意: nothrow The nothrow attribute is used to inform the compiler that a
这个问题在这里已经有了答案: nothrow or exception? (6 个回答) 关闭9年前。 根据C++ reference ,您可以通过以下方式新建对象: MyClass * p1 = n
我是一名优秀的程序员,十分优秀!