C++ 中的错误分配异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4615021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:56:26  来源:igfitidea点击:

Bad allocation exceptions in C++

c++exceptionmingwnew-operatorallocation

提问by CaptainNemo

In a school project of mine I was requested to create a program not using STL.
In the program I use alot of

在我的一个学校项目中,我被要求创建一个不使用 STL 的程序。
在程序中我使用了很多

Pointer* = new Something;
if (Pointer == NULL) throw AllocationError();

My question is about allocation errors:
1. is there an automatic exception thrown by new when allocation fails?
2. if so how can I catch it if I'm not using STL (#include "exception.h)
3. is using the NULL testing enugh?

我的问题是关于分配错误:
1. 分配失败时 new 是否会自动抛出异常?
2. 如果是这样,如果我不使用 STL ( #include "exception.h)
3. 是否使用 NULL 测试足够了?

thank you.
I'm using eclipseCDT(C++) with MinGW on windows 7.

谢谢你。
我在 Windows 7 上使用 eclipseCDT(C++) 和 MinGW。

回答by bcsanches

Yes, the new operator will automatically thrown an exception if it cannot allocate the memory.

是的,如果 new 操作符无法分配内存,它会自动抛出异常。

Unless your compiler disables it somehow, the new operator will never return a NULL pointer.

除非您的编译器以某种方式禁用它,否则 new 运算符永远不会返回 NULL 指针。

It throws a bad_allocexception.

它抛出bad_alloc异常。

Also there is a nothrowversion of new that you can use:

nothrow您还可以使用一个new 版本:

int *p = new(nothrow) int(3);

This version returns a null pointer if the memory cannot be allocated. But also note that this does not guarantee a 100% nothrow, because the constructor of the object can still throw exceptions.

如果无法分配内存,此版本将返回空指针。但也要注意,这并不能保证 100% nothrow,因为对象的构造函数仍然可以抛出异常。

Bit more of information: http://msdn.microsoft.com/en-us/library/stxdwfae(VS.71).aspx

更多信息:http: //msdn.microsoft.com/en-us/library/stxdwfae(VS.71).aspx

回答by Nawaz

  1. is there an autamtic exception thrown by new when allocation fails?
  2. if so how can I catch it if I'm not using STL (#include "exception.h)
  1. 分配失败时,new 是否会引发自动异常?
  2. 如果是这样,如果我不使用 STL (#include "exception.h),我怎么能抓住它

Yes. See this example. It also demonstrates how to catch the exception!

是的。请参阅此示例。它还演示了如何捕获异常!

  try
  {
    int* myarray= new int[10000];
  }
  catch (bad_alloc& ba)
  {
    cerr << "bad_alloc caught: " << ba.what() << endl;
  }

From here : http://www.cplusplus.com/reference/std/new/bad_alloc/

从这里:http: //www.cplusplus.com/reference/std/new/bad_alloc/

3 . is using the NULL testing enugh?

3 . 使用 NULL 测试就够了吗?

That is not needed, unless you overload the newoperator!

这不是必需的,除非您使new运算符过载!

回答by AProgrammer

  1. Yes: std::bad_alloc

  2. In my opinion, that isn't part of the STL any more that operator new is. (You could catch ... but you'll loose the possibility to descriminate with other exceptions).

  3. It is unneeded, new will throw an exception and not return NULL.

  1. 是: std::bad_alloc

  2. 在我看来,这不再是运算符 new 的 STL 的一部分。(你可以赶上......但你会失去用其他例外来区分的可能性)。

  3. 它是不需要的,new 将抛出异常并且不返回 NULL。

回答by 6502

Standard C++ throws an exception if the requested memory cannot be allocated. If you want NULL instead of the exception then the syntax is

如果无法分配请求的内存,标准 C++ 会抛出异常。如果你想要 NULL 而不是异常,那么语法是

Whatever *p = new (std::nothrow) Whatever;

This syntax is just a case of "placement new" allocation that allows an allocator function to receive parameters.

此语法只是“放置新”分配的一种情况,它允许分配器函数接收参数。

Most of the times I've seen checking for NULL after newis in Visual C++ code, where the default behavior of ::operator newis to return NULL instead of raising an exception like the standard requires (this is IMO one of the many areas in which Microsoft tried (is still trying?) to fight against portable code).

大多数情况下,我new在 Visual C++ 代码中看到检查 NULL 之后,其中的默认行为::operator new是返回 NULL 而不是像标准要求那样引发异常(这是 IMO 尝试的众多领域之一(仍在尝试?)与可移植代码作斗争)。

回答by PaulJWilliams

Standard new throws a bad_alloc exception on failure, so your null check isnt needed.

标准 new 在失败时抛出 bad_alloc 异常,因此不需要您的空检查。

回答by VGE

It depends Old c++ compiler provide the set_new_handler to catch allocation failure. You can also catch the bad_alloc exception.

这取决于旧的 c++ 编译器提供 set_new_handler 来捕获分配失败。您还可以捕获 bad_alloc 异常。

http://en.wikipedia.org/wiki/New_%28C%2B%2B%29

http://en.wikipedia.org/wiki/New_%28C%2B%2B%29

If you want to control this you can also override the operator new/operator deletepair

如果你想控制它,你也可以覆盖operator new/operator delete