如何让malloc()失败? [英] How to make malloc() fail ?

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

问题描述

这是关于测试SDK内存的东西。

在什么情况下malloc失败。

任何评论/回复请....

问候

This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards

推荐答案

ni ************ @ gmail.com 写道:

这是关于测试一个SDK内存的东西。

在什么情况下malloc失败。

任何评论/回复请....
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....



声明:


ptr = malloc(N);


当malloc无法分配N时,它将返回NULL

存储的字节数。


强制malloc失败的方法是系统特定的,

所以要求在适当的群体中会更合适。


< OT>

有压力测试程序可以模拟低的免费内存条件。或者,您的操作系统可能具有

设施,用于执行每个用户或每个程序的内存配额

分配。

< / OT>

In statement:

ptr = malloc(N);

malloc will return NULL when it''s not able to allocate N bytes of
storage.

Ways to force malloc to fail in it''s allocation are system specific,
so asking in an appropriate group would be more suitable.

<OT>
There are "stress testing" programs which can simulate a low free
memory condition. Alternatively your operating system might have
facilities for enforcing per user or per program quotas for memory
allocation.
</OT>


santosh说:
santosh said:
ni ************ @ gmail.com 写道:

>这个是关于测试SDK内存的东西。
在什么情况下malloc失败。
任何评论/回复请....
>This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....



In声明:


ptr = malloc(N);


当malloc无法分配N个字节时,它将返回NULL br $>
存储。


强制malloc失败的方法是系统特定的,

所以要求适当的小组会更合适。


In statement:

ptr = malloc(N);

malloc will return NULL when it''s not able to allocate N bytes of
storage.

Ways to force malloc to fail in it''s allocation are system specific,
so asking in an appropriate group would be more suitable.



你看,这就是我不喜欢这个群体。 永远和你在一起

无法完成,正如Yoda正确地说的那样。


好​​吧,从显而易见的意义上说,你是对的。在一般情况下(请求0xFFFFFFFF

字节不算数,当然 - 我们想要更多),不能强制malloc到

在你的命令失败一般解决方案比

那样。


但在我看来,这并不是OP想要的,尽管

他的问题的措辞。他想要的是

中的软件环境,可以为测试目的模拟内存分配失败。

并且*在ISO C中是*可能的。我知道因为我'已经完成了,并且最近也是b $ b(比如,在过去一个月左右)。


我不打算解释如何,因为我不想把注意力集中在他应该开发的更基本的技能上来分散注意力。

开始做一些像编程一样具有智力挑战性的东西。 />

但是我邀请你自己考虑一下这个问题,作为一个锻炼创造性思维。如果你相信我所说的(并且我怀疑你可能是b $ b),你可以这样说:至少存在一个解决方案来确定测试分配的问题便携式C代码中的故障路径;

知道这一点,并且知道我不是傻子,我应该能够自己找到

a解决方案 - 也许它会在我自己的编程中很有用。


你知道它不会很明显。你知道这将需要一点努力。

但是你也知道,如果理查德可以做到这一点,那就不是那么难啊。


我开始对comp.lang.c感到绝望。 : - (


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。+ rjh @

Google用户:< http://www.cpax.org.uk/prg/writings/googly.php>

" Usenet是一个奇怪的地方 - dmr 1999年7月29日

You see, this is what I don''t like about this group. "Always with you it
cannot be done", as Yoda rightly said.

Okay, in the obvious sense, you''re right that you can''t force malloc to
fail at your command, in the general case (requests for 0xFFFFFFFF
bytes don''t count, of course - we want a more general solution than
that).

But it seems to me that this isn''t really what the OP wants, despite the
wording of his question. What he wants is a software environment in
which memory allocation failures can be simulated for testing purposes.
And that *is* possible within ISO C. I know because I''ve done it, and
quite recently too (like, within the last month or so).

I''m not going to explain how, because I wouldn''t want to distract the OP
from focusing on the more basic skills he should be developing before
embarking on something as intellectually challenging as programming.

But I invite you to consider the problem yourself, as an exercise in
creative thinking. If you trust what I say (and I suspect that you
might), you can reason thusly: "there exists at least one solution to
the problem of testing allocation failure paths in portable C code;
knowing this, and knowing that I''m no fool, I ought to be able to find
a solution myself - and maybe it will be useful in my own programming".

You know it won''t be obvious. You know it will involve a little effort.
But you also know that, if Richard can do it, it can''t be *that* hard.

I am beginning to despair of comp.lang.c. :-(

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Richard Heathfield写道:


如果这篇文章出现,我提前道歉多次。谷歌今天很难用到b $ b。
Richard Heathfield wrote:

I apologise in advance if this post appears multiple times. Google has
been wretched to use today.

santosh说:
santosh said:
ni************@gmail.com 写道:

>

这是关于测试SDK内存的东西。

在什么情况下malloc失败了。

任何评论/回复请....
>
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....



声明:


ptr = malloc(N);


当malloc无法分配N个字节时,它将返回NULL

存储。


强制malloc失败的方法是系统特定的,

所以要求适当的群体会更合适。

In statement:

ptr = malloc(N);

malloc will return NULL when it''s not able to allocate N bytes of
storage.

Ways to force malloc to fail in it''s allocation are system specific,
so asking in an appropriate group would be more suitable.



你看,这就是我不喜欢这个群体。 永远和你在一起

无法完成,正如Yoda正确地说的那样。


好​​吧,从显而易见的意义上说,你是对的。在一般情况下(请求0xFFFFFFFF

字节不算数,当然 - 我们想要更多),不能强制malloc到

在你的命令失败一般解决方案比

那样。


但在我看来,这并不是OP想要的,尽管

他的问题的措辞。他想要的是

中的软件环境,可以为测试目的模拟内存分配失败。

并且*在ISO C中是*可能的。我知道因为我'已经完成了,并且最近也是b $ b(比如,在过去一个月左右)。


You see, this is what I don''t like about this group. "Always with you it
cannot be done", as Yoda rightly said.

Okay, in the obvious sense, you''re right that you can''t force malloc to
fail at your command, in the general case (requests for 0xFFFFFFFF
bytes don''t count, of course - we want a more general solution than
that).

But it seems to me that this isn''t really what the OP wants, despite the
wording of his question. What he wants is a software environment in
which memory allocation failures can be simulated for testing purposes.
And that *is* possible within ISO C. I know because I''ve done it, and
quite recently too (like, within the last month or so).



是的。这就是为什么我在便携式C中说迫使malloc的方法,这不是AFAIK,

。正如你所说,模拟的方法

分配失败*在标准C中是可能的。


我仍​​然不太清楚OP想要知道什么

关于。事后看来,我应该向OP指出,仅用标准C来模拟内存分配失败就可以实现
,但是因为我自己通常依赖于为此目的的外部程序,

这个事实让我不知所措。


< snip>

Yes. That''s why I said "ways to force malloc", which isn''t, AFAIK,
possible within portable C. As you''ve noted, ways to simulate
allocation failures *are* possible within Standard C.

I still don''t really understand exactly what the OP wants to know
about. In hindsight, I should have pointed out to the OP that it''s
possible to simulate memory allocation failures with just Standard C,
but since I myself usually rely on external programs for this purpose,
the fact skipped my mind.

<snip>


我开始对comp.lang.c感到绝望。 :-(
I am beginning to despair of comp.lang.c. :-(



我仍​​然认为这是一个优秀的C组,但话说再说一遍,我已经只有两个人才能获得
几年,所以我没有第一手经验

这是鼎盛时期。

I still consider this an excellent group for C, but then again, I''ve
been here only for two years, so I don''t have a first hand experience
of it''s heydays.


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

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