如果malloc失败 [英] If malloc fails

查看:106
本文介绍了如果malloc失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果malloc失败了我该怎么办?


1.立即退出。

2.打印错误信息(或输入日志条目)和退出。

3.打印错误信息(或输入日志条目)并继续执行

执行(在可能从错误中恢复之后)。

在图形环境中打印错误信息可能很困难。


-

邮寄到:randomzoo< at> spymac< dot> com

If malloc fails what should I do?

1. Exit imediately.
2. Print an error message (or put a log entry) and exit.
3. Print an error message (or put a log entry) and continue
execution (after possibly recovering from the error).

Printing an error message might be difficult in a graphical environment.

--
mail to: randomzoo <at> spymac <dot> com

推荐答案

2004年7月19日星期一,H.A。 Sujith写道:


HS>如果malloc失败我该怎么办?

HS>

HS> 1。立即退出。

HS> 2。打印错误消息(或输入日志条目)并退出。

HS> 3。打印错误消息(或输入日志条目)并继续

HS>执行(可能从错误中恢复)。


这取决于stronlgy on the具体应用。对于一次性程序

喜欢''ls''或者一个malloc()失败最有可能是

环境中的一个问题而且只是打印一条消息(如果可能的话) )退出是最好的人可以做的。对于长期运行或在特殊情况下运行的程序(想想控制核电站的软件),这个

不是一个选项,他们可能不得不尝试恢复(和记录,

课程)。


HS>在图形环境中打印错误信息可能很困难。

这就是为什么这些程序有时会从屏幕上消失

而没有任何暗示发生的事情的原因之一。


harti
On Mon, 19 Jul 2004, H.A. Sujith wrote:

HS>If malloc fails what should I do?
HS>
HS>1. Exit imediately.
HS>2. Print an error message (or put a log entry) and exit.
HS>3. Print an error message (or put a log entry) and continue
HS>execution (after possibly recovering from the error).

That depends stronlgy on the concrete application. For one-shot programs
like ''ls'' or so a malloc() failure is most probable a problem in the
environment and just printing a message (if possible) and exiting is the
best one can do. For programs that are long running or run under special
conditions (think of controlling software for a nuclear power plant) this
is not an option and they probably have to try to recover (and log, of
course).

HS>Printing an error message might be difficult in a graphical environment.

That''s one of the reasons why such programs sometimes simply disappear
from the screen without any hint what happend.

harti


2004年7月19日星期一,HA Sujith写道:
On Mon, 19 Jul 2004, H.A. Sujith wrote:
如果malloc失败我该怎么办?

1.立即退出。


也许。

2.打印错误信息(或输入日志条目)并退出。


也许。

3.打印错误信息(或输入日志条目)并继续执行
执行后(可能从错误中恢复) 。


也许。


答案全部取决于具体情况。如果IO绝对必须有一个成功的malloc

或者没有点继续那么我会退出。如果

我试图分配一大块内存,但可以用一个小块b / b小块然后调整并继续。

我是否打印错误信息?取决于环境。如果程序是

将用于自动或嵌入式系统,那么我不会打印

错误信息(没有人可以看到它)。如果应用程序是

总是以交互方式运行,那么我会打印一条错误消息。

在图形环境中打印错误消息可能很困难。
If malloc fails what should I do?

1. Exit imediately.
Maybe.
2. Print an error message (or put a log entry) and exit.
Maybe.
3. Print an error message (or put a log entry) and continue
execution (after possibly recovering from the error).
Maybe.

The answer all depends on the situation. If IO absolutely must have a
successful malloc or there is not point continuing then I would exit. If
I''m attempt to allocate a large block of memory but can get by with a
smaller block then adjust and keep going.

Do I print an error message? Depends on the environment. If the program is
going to be used in an automated or embedded system then I would not print
an error message (there will be no human to see it). If the application is
always going to be run interactively then I would print an error message.
Printing an error message might be difficult in a graphical environment.




为什么?如果只是因为你必须完成创建

对话框的工作,那么学会创建一个对话框。如果是因为失败的malloc

意味着没有足够的内存来显示错误对话框,请查看是否有
是打印错误消息的系统级方式。操作系统通常会为打印关键错误对话框保留内存。如果您的操作系统没有这样做,请在开始时分配足够的内存,以便有内存来打开错误对话框。保留该内存,直到您退出

申请。


-

发送电子邮件至:darrell at cs dot toronto dot edu

不要发送电子邮件至 vi ** **********@whitehouse.gov


HA Sujith写道:
H.A. Sujith wrote:
如果malloc失败我该怎么办?

1.立即退出。
2.打印错误信息(或输入日志条目)和退出。
3.打印错误信息(或输入日志条目)并继续执行
执行(可能从错误中恢复)。

打印错误信息可能很困难在图形环境中。
If malloc fails what should I do?

1. Exit imediately.
2. Print an error message (or put a log entry) and exit.
3. Print an error message (or put a log entry) and continue
execution (after possibly recovering from the error).

Printing an error message might be difficult in a graphical environment.




我已经走下了试图让malloc()失败的途径,而且它更容易了b / b
说完了。此外,实现特定设置必须设置为
以创建malloc()失败的可能性。 (例如,RLIMIT_DATA
linux中的
,BSD中的malloc.conf等)。


一个很好的练习就是尝试将malloc()转换为在您的

实施下失败,以确保您列出的上述项目均有效。并且

不要忘记为尾随的''\ 0'分配足够的空间。


我最近转移到openBSD试验制作malloc()

失败。它确实是特定于实现的,所以我很好奇

常规在这里考虑在平台之间移植malloc()因为那里没有
似乎没有

malloc()失败时,是否设置了errno的标准。


brian



I have gone down the avenue of trying to make malloc() fail, and it''s
easier said then done. Also, implementation specific settings have to
be set to create a possibility of malloc() failing. (e.g., RLIMIT_DATA
in linux, malloc.conf in BSD, etc.).

A good exercise would be just trying to get malloc() to fail under your
implementation to make sure the above items you listed even work. And
do not forget to allocate enough space for the trailing ''\0''.

I have recently moved over to openBSD to experiment with making malloc()
fail. It''s really implementation specific, so I''m curious what the
regulars here think about porting malloc() between platforms since there
doesn''t seem to be a standard as to whether or not errno is set when
malloc() fails.

brian


这篇关于如果malloc失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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