在什么情况下malloc可以返回NULL? [英] Under what circumstances can malloc return NULL?

查看:709
本文介绍了在什么情况下malloc可以返回NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这从来没有发生在我身上,我已经编程多年了.

It has never happened to me, and I've programming for years now.

有人可以给我一个malloc实际上不起作用的非平凡程序的示例吗?

Can someone give me an example of a non-trivial program in which malloc will actually not work?

我不是在谈论内存用尽:我正在寻找一种简单的情况,当您仅按用户指定的绑定大小分配一个内存块时,可以说是一个整数,这会导致malloc失败.

I'm not talking about memory exhaustion: I'm looking for the simple case when you are allocating just one memory block in a bound size given by the user, lets say an integer, causes malloc to fail.

推荐答案

是.

只需尝试malloc超出系统可提供的内存(通过耗尽地址空间或虚拟内存(以较小者为准)).

Just try to malloc more memory than your system can provide (either by exhausting your address space, or virtual memory - whichever is smaller).

malloc(SIZE_MAX)

可能会做到.如果没有,请重复几次,直到用完为止.

will probably do it. If not, repeat a few times until you run out.

这篇关于在什么情况下malloc可以返回NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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