malloc真的会失败吗? [英] Can malloc ever really fail?

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

问题描述

如果(a = malloc(...)== NULL){

/ /错误代码

}


我的意思是,malloc调用真的可能会失败,除非在

的情况下耗尽虚拟内存?


-Chess

解决方案

国际象棋Saurus< ch *** *****@yahoo.com>这样说:

我的意思是,除了在虚拟内存不足的情况下,malloc调用是否真的可能失败?




为什么不读几篇关于这个非常好的b $ b主题的讨论呢? (是的,它可能会失败。)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


在< e7 ************************** @发布.google.com> ch********@yahoo.com (Chess Saurus)写道:
< blockquote class =post_quotes>我有点厌倦写作

if(a = malloc(...)== NULL){


我希望你不要在真实代码中这样写它!你肯定

不想让0或1变量a。

//错误代码


我是的,malloc调用真的可能会失败,除非用完虚拟内存吗?




你有合同吗?上帝(或撒旦或其他什么)你的

程序永远不会在一个耗尽虚拟

内存的系统上执行?


如果适合您的应用,您可以执行以下操作:


void * xmalloc(size_t size)

{

void * p = malloc(size);

if(p == NULL){

/ *错误代码* /

退出(EXIT_FAILURE) ; / *或abort(); * /

}

返回p;

}


并使用xmalloc而不是malloc,没有检查它的回报

值。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de


国际象棋Saurus< ch ******** @ yahoo.com>写道:

我有点厌倦了写作




然后不要用C.或者有些人写一个xmalloc()包装器

它的作用如下:


void * xmalloc(size_t len){

void * p = malloc(len);


if(p == NULL)

退出(EXIT_FAILURE);


返回p;

}


当然,不要忘记在你的代码/应用程序上放一个大的免责声明

其中说:在任何情况下都不能依靠这个程序来获得除了你自己娱乐之外的其他任何东西。


I''m getting a little bit tired of writing

if (a = malloc(...) == NULL) {
// error code
}

I mean, is it really possible that a malloc call could fail, except in
the case of running out of virtual memory?

-Chess

解决方案

Chess Saurus <ch********@yahoo.com> spoke thus:

I mean, is it really possible that a malloc call could fail, except in
the case of running out of virtual memory?



Why not read a few threads down for a lovely discussion of this very
topic? (Yes, it can fail.)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


In <e7**************************@posting.google.com > ch********@yahoo.com (Chess Saurus) writes:

I''m getting a little bit tired of writing

if (a = malloc(...) == NULL) {
I hope you don''t write it like this in your real code! You certainly
don''t want to assing 0 or 1 to variable a.
// error code
}

I mean, is it really possible that a malloc call could fail, except in
the case of running out of virtual memory?



Do you have a contract with God (or Satan or whatever) that your
programs will NEVER be executed on a system running out of virtual
memory?

If appropriate to your application, you can do the following:

void *xmalloc(size_t size)
{
void *p = malloc(size);
if (p == NULL) {
/* error code */
exit(EXIT_FAILURE); /* or abort(); */
}
return p;
}

and use xmalloc instead of malloc, without ever checking its return
value.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de


Chess Saurus <ch********@yahoo.com> wrote:

I''m getting a little bit tired of writing



Then don''t use C. Or alternatively some people write an xmalloc() wrapper
which does something like:

void *xmalloc(size_t len) {
void *p = malloc(len);

if (p == NULL)
exit(EXIT_FAILURE);

return p;
}

Of course, don''t forget to put a big fat disclaimer on your code/application
which says: Don''t under any circumstances depend on this program for
anything other than your own amusement.


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

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