C++ 中的错误分配异常
在我的一个学校项目中,我被要求创建一个不使用STL的程序
在程序中,我使用了很多
Pointer* = new Something;
if (Pointer == NULL) throw AllocationError();
我的问题是关于分配错误:
- 是否有一个分配失败时
new
抛出自动异常? - 如果是这样,如果我不使用 STL (
#include "exception.h"
),我该如何捕获它? 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:
- is there an automatic exception thrown by
new
when allocation fails? - if so, how can I catch it if I'm not using STL (
#include "exception.h"
)? - is
NULL
testing enough?
Thank You.
I'm using eclipseCDT(C++) with MinGW on Windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,如果new操作符无法分配内存,它会自动抛出异常。
除非你的编译器以某种方式禁用它,否则 new 运算符将永远不会返回 NULL 指针。
它抛出一个
bad_alloc
异常。另外,您还可以使用 new 的
nothrow
版本:如果无法分配内存,该版本将返回空指针。但还要注意,这并不能保证 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: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
是的。请参阅此示例。它还演示了如何捕获异常!
从这里: http://www.cplusplus.com/reference/std/new/bad_alloc/
这是不需要的,除非您重载 new 运算符!
Yes. See this example. It also demonstrates how to catch the exception!
From here : http://www.cplusplus.com/reference/std/new/bad_alloc/
That is not needed, unless you overload the
new
operator!是的:std::bad_alloc
在我看来,这不再是operator new 的STL 的一部分。 (您可以捕获...,但您将失去区分其他异常的可能性)。
不需要,new会抛出异常,不返回NULL。
Yes: std::bad_alloc
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).
It is unneeded, new will throw an exception and not return NULL.
如果无法分配所请求的内存,标准 C++ 将引发异常。如果您想要 NULL 而不是异常,则语法为
此语法只是“放置新”分配的一种情况,允许分配器函数接收参数。
大多数时候,我在 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
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).标准 new 在失败时会引发 bad_alloc 异常,因此不需要进行空检查。
Standard new throws a bad_alloc exception on failure, so your null check isnt needed.
这取决于
旧的 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