C++ 中的错误分配异常

发布于 2024-10-11 06:59:38 字数 520 浏览 6 评论 0原文

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

在程序中,我使用了很多

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

我的问题是关于分配错误:

  1. 是否有一个分配失败时new抛出自动异常
  2. 如果是这样,如果我不使用 STL (#include "exception.h"),我该如何捕获它?
  3. NULL 测试是否足够?

谢谢。
我在 Windows 7eclipseCDT(C++)MinGW >。

In a school project of mine, I was requested to create a program without using STL

In the program, I use a lot of

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

My questions are about allocation error:

  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 NULL testing enough?

Thank You.
I'm using eclipseCDT(C++) with MinGW on Windows 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里南柯 2024-10-18 06:59:38

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

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

它抛出一个 bad_alloc 异常。

另外,您还可以使用 new 的 nothrow 版本:

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

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

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

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

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

It throws a bad_alloc exception.

Also there is a nothrow version of new that you can use:

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.

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

飘逸的'云 2024-10-18 06:59:38
  1. 分配失败时new是否会自动抛出异常?
  2. 如果是这样,如果我不使用 STL (#include "exception.h),我怎样才能捕获它

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

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

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

3.正在使用 NULL 测试
够了吗?

这是不需要的,除非您重载 new 运算符!

  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)

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/

3 . is using the NULL testing
enugh?

That is not needed, unless you overload the new operator!

带刺的爱情 2024-10-18 06:59:38
  1. 是的:std::bad_alloc

  2. 在我看来,这不再是operator new 的STL 的一部分。 (您可以捕获...,但您将失去区分其他异常的可能性)。

  3. 不需要,new会抛出异常,不返回NULL。

  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.

心欲静而疯不止 2024-10-18 06:59:38

如果无法分配所请求的内存,标准 C++ 将引发异常。如果您想要 NULL 而不是异常,则语法为

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

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

大多数时候,我在 Visual C++ 代码中看到在 new 之后检查 NULL,其中 ::operator new 的默认行为是返回 NULL 而不是引发像标准要求的异常(在我看来,这是微软尝试(仍在尝试?)对抗可移植代码的众多领域之一)。

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

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 new is in Visual C++ code, where the default behavior of ::operator new is 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).

无所谓啦 2024-10-18 06:59:38

标准 new 在失败时会引发 bad_alloc 异常,因此不需要进行空检查。

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

浮光之海 2024-10-18 06:59:38

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

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

如果您想控制它,您还可以覆盖 operator new/operator delete

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文