C中的malloc()何时返回NULL? [英] When does malloc() in C return NULL?

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

问题描述

当堆内存不足或堆超碎片时,Malloc返回null.

Malloc returns a null when heap memory is insufficient OR when heap is super fragmented.

我想知道的是,如果在malloc()返回NULL的情况下还有其他情况?

What I would like to know is that if there are OTHER circumstances when malloc() returns a NULL?

PS:在什么情况下malloc可以返回NULL?似乎没有回答我的问题

PS:Under what circumstances can malloc return NULL? didn't seem to answer my question

推荐答案

C中的malloc()什么时候返回NULL?

malloc()在未能分配所需空间时返回空指针.

malloc() returns a null pointer when it fails to allocate the needed space.

这可能是由于:

  1. 计算机内存不足(字节不足)
  2. 该进程的内存不足(操作系统可能会限制每个进程的空间)
  3. 内存不足(分配过多,某些分配器具有此限制)
  4. 碎片太多(存在足够的内存,但是分配器不能/不想将其重新组织成一个连续的块).
  5. 诸如您的流程之类的各种原因都是不值得更多.
  1. Out-of-memory in the machine (not enough bytes)
  2. Out-of-memory for the process (OS may limit space per process)
  3. Out of memory handles (Too many allocations, some allocators has this limit)
  4. Too fragmented (Enough memory exist, but allocator can't/does not want to re-organize into a continuous block).
  5. All sorts of reasons like your process is not worthy of more.

malloc(0) 可能返回空指针. C17/18加了一点.

malloc(0) may return a null pointer. C17/18 adds a bit.

如果请求的空间大小为零,则行为是实现定义的:
返回空指针以指示错误
否则行为就好像大小是某个非零值,只是返回的指针不得用于访问对象.

If the size of the space requested is zero, the behavior is implementation-defined:
either a null pointer is returned to indicate an error,
or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

malloc(0) 可能返回空指针. (C17/18之前的版本)

malloc(0) may return a null pointer. (pre-C17/18)

如果请求的空间大小为零,则行为是实现定义的:
返回一个空指针,
否则行为就好像大小是某个非零值,只是返回的指针不得用于访问对象.

If the size of the space requested is zero, the behavior is implementation-defined:
either a null pointer is returned,
or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.

C17/18的表示错误" 对我而言意味着 null指针返回是错误,可能是由于上述5个原因之一和没有错误的malloc(0)不会返回空指针.

The "to indicate an error" of C17/18 implies to me that a null pointer return is an error, perhaps due to one of the above 5 reasons and a non-erroring malloc(0) does not return a null pointer.

我认为这是趋向于使p = malloc(n); if (p==NULL) error();在出错时始终为true的趋势,即使n为0.其他人可能将其编码为if (p==NULL && n > 0) error();

I see this as a trend to have p = malloc(n); if (p==NULL) error(); to always be true on error even if n is 0. Else one might code as if (p==NULL && n > 0) error();

如果代码要允许零分配以将NULL作为非错误返回,则比调用malloc()更好地形成一个辅助函数以测试n == 0并返回NULL.

If code wants to tolerate an allocation of zero to return NULL as a non-error, better to form a helper function to test for n == 0 and return NULL than call malloc().

相反,返回非 null指针并不总是意味着这是足够的内存.请参见为什么malloc不用完"我计算机上的内存?

Conversely a return of non-null pointer does not always mean this is enough memory. See Why is malloc not "using up" the memory on my computer?

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

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