非故障保证malloc / new [英] Non-failure guarantied malloc/new

查看:47
本文介绍了非故障保证malloc / new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我见过很多C和C ++代码(不是我自己的代码),它们没有做任何

检查new或malloc获取的内存是否有效或是否返回NULL

指针。

为什么大多数人不关心做这种错误检查?


当我以前学习语言我被告知要经常检查if(p!=

NULL)"否则就会返回一个错误。

我仍​​然会这样做,但有时候如果我想要在构造函数中分配内存并且想要在内部构造函数中分配内存,那就太烦人了无法向调用者返回

错误代码以告知出现问题。


考虑到这个问题让我做了一些像运行内存的黑客

在while()循环中分配,例如:


blah * p = NULL;

while(!p)

{

p = malloc(...);

}


除了整个事情之外我们得到了新的运算符,它在每个编译器实现的

上工作方式不同。有些编译器会抛出异常而其他编译器会抛出NULL指针。实际上我更喜欢NULL指针,因为它太多了

更容易在while循环中处理然后做一些异常处理。


我希望我''我现在已经达到了我的观点,因为操作系统确实耗尽了内存。为什么

如果可以避免,我会让我的程序崩溃?我最近只是为了好玩

编写了一个程序,它在操作系统中占用了我所有的内存然后启动了一个

程序,我知道它没有对新操作符进行任何错误检查。猜猜

什么,它在没有时间崩溃。

在我看来,运行while()循环并停止程序更好

然后让它崩溃,没有?


我正在寻找建议或可能的内存实施

分配,保证不会失败。


提前致谢。

- John

解决方案

< blockquote>" John Eskie" <乔******** @ intheunknown.net>写了...

最近我见过很多C和C ++代码(不是我自己的代码),它们没有做任何检查是否由new或malloc获得的内存有效或者如果他们返回
NULL指针。
为什么大多数人不关心做这种错误检查?


可能是因为内存很便宜而且程序从你看过的代码中得到的b
应该有很多。只是一个猜测...

当我习惯学习语言时,我总是要检查if(p!=
NULL)。否则返回错误。


这不是一个糟糕的风格。

我仍​​然会这样做,但有时候如果我这样做会让我感到烦恼想要在构造函数中分配内存,并且无法向调用者返回
错误代码以通知出现问题。


抛出异常。这是表示未能创建

对象的常用方法。

考虑到这个问题让我做了一些黑客,比如运行内存分配一个while()循环,例如:

blah * p = NULL;
while(!p)
{
p = malloc(...);
}


要做什么?

除了整个事情之外,我们得到了新的运算符,它在每个编译器实现上的工作方式不同。有些编译器会抛出异常而其他编译器会给出NULL指针。


那些给出NULL指针的是不合规的。

实际上我更喜欢NULL指针,因为它更容易处理一个while循环然后做一些异常处理。


然后你需要使用nothrow新运营商的形式。

我希望我现在已经达到了我的观点,因为操作系统确实耗尽了内存。为什么如果我可以避免它,我会让我的程序崩溃?为了好玩,我最近写了一个程序,它在操作系统中占用了我所有的记忆,然后启动了一个
程序,我知道它没有对新操作符进行任何错误检查。
猜猜是什么,它在没有时间崩溃。


当然。它还能做什么?

在我看来,运行while()循环并停止
程序然后让它崩溃,不是吗?


我不确定你的意思。程序

让我感到非常生气,当我需要它们时它们没有响应,并且必须手动杀死它们。

这怎么比崩溃更好?

我正在寻找建议或可能实现的内存分配,保证不会失败。




没有这样的事情。由于内存是有限的资源,一些内存

分配必然会失败。


Victor


John Eskie写道:

最近我见过很多C和C ++代码(不是我自己的代码),如果新的或malloc获得的内存是有效或者如果它们返回NULL
指针。
为什么大多数人不关心这种错误检查?


也许是因为新可以抛出异常吗?

当我习惯学习语言时,我总是要检查if(p!=
NULL)。否则返回错误。


您使用C吗?

我仍​​然会这样做,但有时如果我想要的话,它会让我感到烦恼在构造函数中分配内存,并且无法向调用者返回错误代码以通知出现问题。


抛出异常。

考虑到这个问题让我做了一些黑客,比如在while()循环中运行内存分配,例如:

blah * p = NULL;
while(!p)
{
p = malloc(...);
}

最重要的是,我们得到了新的运算符,它在每个编译器实现上的工作方式不同。有些编译器会抛出异常而其他编译器会给出NULL指针。实际上我更喜欢NULL指针,因为它在while循环中更容易处理,然后进行一些异常处理。


我完全不同意。如果您确实遇到问题,请包含< new>

并使用set_new_handler()设置失败时的行为。

我希望我''我现在已经达到了我的观点,因为操作系统确实耗尽了内存。为什么如果我可以避免它,我会让我的程序崩溃?为了好玩,我最近写了一个程序,它在操作系统中占用了我所有的记忆,然后启动了一个
程序,我知道它没有对新操作符进行任何错误检查。猜猜是什么,它在没有时间崩溃。


惊喜,惊喜。 :)

在我看来,运行while()循环并停止程序更好然后让它崩溃,不是吗?


我想是的,但我并不喜欢这两种选择。如果

进程的可用性不是cricital,我通常会打印错误

消息,然后尝试尽可能优雅地退出。当然,如果该程序没有死亡,那么所需资源必须非常谨慎地指定,并且程序可以尝试将它们全部保留在

启动。

我正在寻找建议或可能实现的内存分配,保证不会失败。


您可以在静态块中分配所有需要的内存。当然,然后

你的程序的每次运行将使用与最坏情况一样多的内存。

你的while循环想法可能是下一个最接近故障证明

分配器。

提前致谢。
- John




- 杰夫




" John Eskie" <乔******** @ intheunknown.net>在消息中写道

news:3f *********************** @ dread16.news.tele.d k ... < blockquote class =post_quotes>最近我见过很多C和C ++代码(不是我自己的代码),如果新的或malloc获得的内存有效或者返回$,它们都不会检查b $ b NULL指针。
为什么大多数人不关心做这种错误检查?


因为:

1.对于虚拟内存来说,一个程序耗尽

内存非常罕见。

2.如果没有内存,则抛出bad_alloc

3.持续检查空指针的代码浪费了
程序内存和CPU和意志导致更多的分页,因为每页上的实际代码会少一些。

4.如果你用完了,那么通常不可能做任何明智的事情。 >
记忆。特别是你不能轻易地做标准的I / O来告诉任何人关于它的
你当然不能做任何GUI的东西。

5.无论你做什么你都不能使你调用的库使用相同的

系统,它们通常包含比你的
应用程序更多的代码。

我曾经习惯于学习我总是要检查的语言if(p!=
NULL)。否则返回错误。


如果您正在为Araine 5编写控制软件,那么它可能很好

建议 - 对于我们大多数人写的我准备下注的软件

很多钱,你会在生产代码中遇到你的第一百万个真正的错误

,然后你会因为内存不足而有关于崩溃的真实错误报告。

我仍​​然一直这样做,但有时候如果我想在构造函数中分配内存而无法向调用者返回一个
错误代码,那就太烦人了。告知出了问题。


新抛出bad_alloc

想到这个问题让我做了一些黑客,比如在while()循环中运行内存分配,例如: br />
blah * p = NULL;
while(!p)
{
p = malloc(...);
}
<在整个事情的基础上,我们得到了新的运算符,它在每个编译器实现上的工作方式不同。有些编译器会抛出异常而其他编译器会给出NULL指针。实际上我更喜欢NULL指针,因为它在while循环中更容易处理,然后进行一些异常处理。


我不明白为什么但是因为你需要ctors的例外我

建议你也用它们进行内存分配。


如果你坚持检查null即使是uptodate实现那么

只需使用:

#include< new>

p = new(nothrow)X();

我希望我现在已达到我的观点,因为操作系统的内存不足。为什么如果我可以避免它,我会让我的程序崩溃?为了好玩,我最近写了一个程序,它在操作系统中占用了我所有的记忆,然后启动了一个
程序,我知道它没有对新操作符进行任何错误检查。
猜猜是什么,它在没有时间崩溃。
在我看来,运行while()循环并停止
程序然后让它崩溃,不是吗?


no

我正在寻找建议或可能实施的内存分配,保证不会失败。


预分配内存池,你知道这些内存池就足够了,并且可以从

分配它们 - 但要在整个应用程序中执行此操作是非常困难的并且
如果您使用第三方图书馆,则不可能


提前致谢。
- John



Lately I''ve seen alot of C and C++ code (not my own) which doesn''t do any
checking if memory obtained by new or malloc is valid or if they return NULL
pointers.
Why does most people not care about doing this kind of error checking?

When I used to learn the language I was told always to check "if (p !=
NULL)" and return a error otherwise.
I still do that always but sometimes it''s annoying the hell out of me if I
want to allocate memory inside a constructor and have no way to return an
error code to the caller to inform that something went wrong.

Thinking about this issue made me do some hacks like running memory
allocation in a while() loop such as:

blah *p = NULL;
while (!p)
{
p = malloc(...);
}

On top of the whole thing we got new operator which works differently on
each compiler implementation. Some compilers throw exceptions while others
give NULL pointers. Actually I prefer NULL pointers because it''s so much
easier to handle in a while loop then doing some exception handling.

I hope I''ve reached my point now because OS''s do run out of memory. Why
would I let my program crash if I can avoid it? Just for fun I recently
wrote a program that took all my memory in the OS and then started one
program which I knew did not have any error checking on new operator. Guess
what, it crashes in no-time.
In my opinion it''s still better to run a while() loop and stall the program
then having it crash instead, no?

I am looking for suggestions or possible implementations of memory
allocation which is guarrantied not to fail.

Thanks in advance.
-- John

解决方案

"John Eskie" <jo********@intheunknown.net> wrote...

Lately I''ve seen alot of C and C++ code (not my own) which doesn''t do any
checking if memory obtained by new or malloc is valid or if they return NULL pointers.
Why does most people not care about doing this kind of error checking?
Probably because memory is cheap and computers on which the programs made
from the code you''ve seen are supposed to have lots of it. Just a guess...
When I used to learn the language I was told always to check "if (p !=
NULL)" and return a error otherwise.
It''s not a bad style.
I still do that always but sometimes it''s annoying the hell out of me if I
want to allocate memory inside a constructor and have no way to return an
error code to the caller to inform that something went wrong.
Throw an exception. It''s the usual way to indicate a failure to create
an object.
Thinking about this issue made me do some hacks like running memory
allocation in a while() loop such as:

blah *p = NULL;
while (!p)
{
p = malloc(...);
}
To do what?
On top of the whole thing we got new operator which works differently on
each compiler implementation. Some compilers throw exceptions while others
give NULL pointers.
Those that give NULL pointers are non-compliant.
Actually I prefer NULL pointers because it''s so much
easier to handle in a while loop then doing some exception handling.
Then you need to use a "nothrow" form of the new operator.
I hope I''ve reached my point now because OS''s do run out of memory. Why
would I let my program crash if I can avoid it? Just for fun I recently
wrote a program that took all my memory in the OS and then started one
program which I knew did not have any error checking on new operator. Guess what, it crashes in no-time.
Sure. What else could it do?
In my opinion it''s still better to run a while() loop and stall the program then having it crash instead, no?
I am not sure what you mean by that. I get really annoyed by programs
that do not respond when I need them to, and have to kill them manually.
How is that better than crashing?
I am looking for suggestions or possible implementations of memory
allocation which is guarrantied not to fail.



No such thing. Since memory is a limited resource, some memory
allocations are bound to fail.

Victor


John Eskie wrote:

Lately I''ve seen alot of C and C++ code (not my own) which doesn''t do any
checking if memory obtained by new or malloc is valid or if they return NULL
pointers.
Why does most people not care about doing this kind of error checking?
Perhaps because "new" can throw exceptions?
When I used to learn the language I was told always to check "if (p !=
NULL)" and return a error otherwise.
Were you using C?
I still do that always but sometimes it''s annoying the hell out of me if I
want to allocate memory inside a constructor and have no way to return an
error code to the caller to inform that something went wrong.
Throw an exception.
Thinking about this issue made me do some hacks like running memory
allocation in a while() loop such as:

blah *p = NULL;
while (!p)
{
p = malloc(...);
}

On top of the whole thing we got new operator which works differently on
each compiler implementation. Some compilers throw exceptions while others
give NULL pointers. Actually I prefer NULL pointers because it''s so much
easier to handle in a while loop then doing some exception handling.
I disagree entirely. If you''re really having a problem, include <new>
and use set_new_handler() to set the behavior you want on failure.
I hope I''ve reached my point now because OS''s do run out of memory. Why
would I let my program crash if I can avoid it? Just for fun I recently
wrote a program that took all my memory in the OS and then started one
program which I knew did not have any error checking on new operator. Guess
what, it crashes in no-time.
Surprise, surprise. :)
In my opinion it''s still better to run a while() loop and stall the program
then having it crash instead, no?
I suppose so, but I don''t really like either of those options. If
availability of the process is not cricital, I usually print an error
message, then try to exit as gracefully as possible. Of course, if it''s
essential that the program not die, the resources needed must be
specified very carefully, and the program can try to reserve them all on
startup.
I am looking for suggestions or possible implementations of memory
allocation which is guarrantied not to fail.
You could allocate all needed memory in static blocks. Of course, then
every run of your program will use as much memory as the worst case.
Your while-loop idea may be the next closest thing to a "fail-proof"
allocator.
Thanks in advance.
-- John



-Jeff



"John Eskie" <jo********@intheunknown.net> wrote in message
news:3f***********************@dread16.news.tele.d k...

Lately I''ve seen alot of C and C++ code (not my own) which doesn''t do any
checking if memory obtained by new or malloc is valid or if they return NULL pointers.
Why does most people not care about doing this kind of error checking?
Because:
1. with virtual memory it extremely uncommon for a program to run out of
memory.
2. new throws bad_alloc if there is no memory
3. The code for continually checking for null pointers is wasteful of
program memory and CPU and will cause more paging because there will be less
real code on each page.
4. It is not generally possible to do anything sensible if you do run out of
memory. In particular you cannot portably do standard I/O to tell anyone
about it and you certainly can''t do any GUI stuff.
5. Whatever you do you cannot make the libraries that you call use the same
system and they will typically contain way more code than your part of the
application.

When I used to learn the language I was told always to check "if (p !=
NULL)" and return a error otherwise.
If you are writing control software for Araine 5 then its probably good
advice - for teh sort of software that most of us write I prepared to bet a
lot of money that you will hit your millionth real bug in production code
before you have a real bug report about crashing because of lack of memory.
I still do that always but sometimes it''s annoying the hell out of me if I
want to allocate memory inside a constructor and have no way to return an
error code to the caller to inform that something went wrong.
new throws bad_alloc

Thinking about this issue made me do some hacks like running memory
allocation in a while() loop such as:

blah *p = NULL;
while (!p)
{
p = malloc(...);
}

On top of the whole thing we got new operator which works differently on
each compiler implementation. Some compilers throw exceptions while others
give NULL pointers. Actually I prefer NULL pointers because it''s so much
easier to handle in a while loop then doing some exception handling.
I don''t understand why but since you need exceptions for ctors anyway I
suggest you use them for memory allocation as well.

If you insist on checking for null even with uptodate implementations then
just use:
#include <new>
p = new(nothrow) X();

I hope I''ve reached my point now because OS''s do run out of memory. Why
would I let my program crash if I can avoid it? Just for fun I recently
wrote a program that took all my memory in the OS and then started one
program which I knew did not have any error checking on new operator. Guess what, it crashes in no-time.
In my opinion it''s still better to run a while() loop and stall the program then having it crash instead, no?
no

I am looking for suggestions or possible implementations of memory
allocation which is guarrantied not to fail.
Preallocate memory pools that you know will be sufficient and allocate from
them - but to do this across a whole application is ludicrously hard and
impossible if you use 3rd party libraries

Thanks in advance.
-- John



这篇关于非故障保证malloc / new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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