异常检查内存分配。怎么样? [英] Exception checking memory allocation. How?

查看:56
本文介绍了异常检查内存分配。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常的做法(我听说)始终从新中获取

分配例外情况。如何在

练习中完成?发生此类事件后,常见的程序(

序列的路径)会是什么? (对于

数百万LOC,基于GUI的应用程序)。


这也是不分配对象的常见建议

带有new的除非绝对必要。替代方案

是将对象保留在堆栈中,自动分配

并通过范围控制取消分配。在这种情况下,为了防止分配错误(即堆栈

溢出),我使用了哪些机制




谢谢!

解决方案

>通常的做法(我听说)始终从新中捕获

分配例外。如何在
练习中完成?发生此类事件后,常见程序(序列的路径)会是什么? (对于数百万LOC,基于GUI的应用程序)。


我不知道共性但是分配了抛出的异常对象

堆真的似乎没什么好处。堆栈溢出你不会有C ++

异常。此外,如果在垃圾收集器不存在的情况下新建,则必须删除抛出的

异常。


大部分时间它都是抛出的对象类型很重要。只有

偶尔我必须检查

异常所带来的内部信息。

这也是一个不分配对象的常见建议
withnew除非绝对必要。另一种方法是将对象保留在堆栈中,自动分配
并通过范围控制取消分配。在这种情况下我使用了哪些机制来防止分配错误(即堆栈溢出)?




这实际上取决于如何对象将被使用。以下是一些事实:

*当执行超出范围时,堆栈会弹出,因此你可以将
保留在超出范围的堆栈上。但是,您不必担心手动删除对象。

*在大多数系统中,堆栈分配比其他方式更有效。

*堆栈是有限的,通常比你可以获得的最大堆小得多

。对于小物体,这不是问题。但是对于大型物体,应该照顾好b $ b。通常的做法是使用RAII(例如,

std :: string)


Ben


< BLOCKQUOTE>>这实际上取决于对象的使用方式。以下是一些事实:

*当执行超出范围时,会弹出堆栈,因此您可以保留超出范围的堆栈中的任何内容。但是,你没有
担心手动删除对象。




我的意思是你不能保留堆栈范围之外的任何东西

课程。请原谅我的错字。


Ben


Jacob写道:

这是常见的练习(我听说)总是从新中获取分配异常。


并非总是如此。在一个环境中使用的小程序中,当有足够的内存时,总是可以使用
,并且具有一些通用感的用户,你可以让程序中止



如何在实践中完成?发生此类事件后,常见程序(序列的路径)会是什么? (对于数百万的LOC,基于GUI的应用程序)。


通常的程序是根据项目设计者写的指南

做一些事情。

它也是不用新分配对象的常见建议
除非绝对必要。另一种方法是将对象保留在堆栈中,自动分配
并通过范围控制取消分配。在这种情况下,我使用了哪些机制来防止分配错误(即堆栈溢出)?




在需要时使用new分配objetcs ,并忘记绝对这个词。

研究你的平台的局限性和他的非标准方式

检查它。


-

Salu2


It is common practice (I''ve heard) to always catch
allocation exceptions from "new". How is done in
practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a
multi-million LOC, GUI based application).

It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?

Thanks!

解决方案

> It is common practice (I''ve heard) to always catch

allocation exceptions from "new". How is done in
practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a
multi-million LOC, GUI based application).
I don''t know about commonality but allocating the exception object thrown on
the heap really doesn''t seem to have any advantages. You won''t have a C++
exception for stack overflow. And besides, you have to delete the thrown
exception if it is newed in a context where a garbage collector is absent.

Most of the time it is the type the object thrown that is important. Only
occasionally I have to check the internal information carried by the
exception thrown.

It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?



It really depends on how the object is to be used. Here are some facts:
*The stack is popped off when the execution goes out of scope, so you can
preserve anything on the stack beyond the scope. However, you don''t have to
worry about manually deleting the object.
*Stack allocation is much more efficient than other means in most systems.
*The stack is limited, often much smaller than max heap you can acquire
mostly. For small objects, this isn''t an issue. But for large objects it
should be taken care of. A common practice is to utilize RAII (for example,
std::string)

Ben


> It really depends on how the object is to be used. Here are some facts:

*The stack is popped off when the execution goes out of scope, so you can
preserve anything on the stack beyond the scope. However, you don''t have to worry about manually deleting the object.



I meant you CANNOT preserve anything on the stack beyond the scope of
course. Excuse my typo.

Ben


Jacob wrote:

It is common practice (I''ve heard) to always catch
allocation exceptions from "new".
Not always. In a small program used in a environment when enough memory is
always available, and wit users with some commnon sense, you can just let
the program abort.
How is done in practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a multi-million LOC,
GUI based application).
The common procedure is to do something according to the guidelines written
by the designers of the project.
It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?



Allocate objetcs with new when required, and forget the word "absolutely".
Study the limitations of your platform and his non-standard ways of
checking it.

--
Salu2


这篇关于异常检查内存分配。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆