什么是应该的realloc失败的可能性有多大? [英] What are the chances that realloc should fail?

查看:444
本文介绍了什么是应该的realloc失败的可能性有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它运行的类似的可用内存来的malloc是否失败或有可能有其他原因?

Does it fail when it runs out of free memory similar to malloc or could there be other reasons?

推荐答案

任意分配功能(的malloc 的realloc 释放calloc ,并在POSIX, posix_memalign )可能失败的以下任一原因,可能还有其他:

Any of the allocation functions (malloc, realloc, calloc, and on POSIX, posix_memalign) could fail for any of the following reasons, and possibly others:


  • 您已经使用了你的整个虚拟地址空间,或至少它的可用部分。在32位机,也有只有4GB值得的地址,并可能1GB左右被保留用于操作系统内核使用。即使你的机器具有的物理内存16GB,单个进程不能使用超过它有地址。

  • 您没有使用你的虚拟地址空间,但是你已经如此糟糕,请求大小的地址不连续的范围可分散的IT。这可能发生(32位计算机上),如果你成功地分配512MB的6块,免费每隔一个,那么尝试分配1GB的块。当然还有很多其他的例子具有较小存储容量。

  • 你的机器已用尽物理内存
  • ,无论是由于有使用这一切,还是在机器上运行的其他程序自己的程序有使用这一切。有些系统(Linux的默认配置)会的过量的,这意味着的malloc 不会因此而失败,而是操作系统稍后会杀一或多个程序时,它计算出有没有真正足够的物理内存来绕去。但在强大的系统(包括Linux与过量使用禁用),的malloc 将如果有没有留下物理内存失败。

  • You've used up your entire virtual address space, or at least the usable portion of it. On a 32-bit machine, there are only 4GB worth of addresses, and possibly 1GB or so is reserved for use by the OS kernel. Even if your machine has 16GB of physical memory, a single process cannot use more than it has addresses for.
  • You haven't used up your virtual address space, but you've fragmented it so badly that no contiguous range of addresses of the requested size are available. This could happen (on a 32-bit machine) if you successfully allocate 6 512MB blocks, free every other one, then try to allocate a 1GB block. Of course there are plenty of other examples with smaller memory sizes.
  • Your machine has run out of physical memory, either due to your own program having used it all, or other programs running on the machine having used it all. Some systems (Linux in the default configuration) will overcommit, meaning malloc won't fail in this situation, but instead the OS will later kill one or more programs when it figures out there's not really enough physical memory to go around. But on robust systems (including Linux with overcommit disabled), malloc will fail if there's no physical memory left.

注意,严格来说,分配函数也可以在任何时间以任何理由失败。最大限度地减少故障是质量的执行问题。这也有可能是的realloc 可能会失败,甚至当的降低的对象的大小;这可能发生在实现由规模严格分开分配。当然,在这种情况下,你可以简单地继续使用旧的(大)的对象。

Note that strictly speaking, the allocation functions are allowed to fail at any time for any reason. Minimizing failure is a quality-of-implementation issue. It's also possible that realloc could fail, even when reducing the size of an object; this could happen on implementations that strictly segregate allocations by size. Of course, in this case you could simply continue to use the old (larger) object.

这篇关于什么是应该的realloc失败的可能性有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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