简体   繁体   English

如果没有连续的内存空间,realloc会怎么做?

[英]What would realloc do if there is no sequential space of memory?

realloc is used to reallocate the memory dynamically. realloc用于动态重新分配内存。

Suppose I have allocated 7 bytes using the malloc function and now I want to extend it to 30 bytes. 假设我使用malloc函数分配了7个字节,现在我想将它扩展到30个字节。

What will happen in the background if there is no sequential (continously in a single row) space of 30 bytes in the memory? 如果内存中没有30个字节的连续(连续单行)空间,后台会发生什么?

Is there any error or will memory be allocated in parts? 是否有任何错误或内存将被分配?

realloc works behind the scenes roughly like this: realloc在幕后工作大致如下:

  • If there is enough free space behind the current block to fulfill the request, extend the current block and return a pointer to the beginning of the block. 如果当前块后面有足够的可用空间来满足请求,则扩展当前块并返回指向块开头的指针。
  • Else if there is a large enough free block elsewhere, then allocate that block, copy the data from the old block over, free the old block and return a pointer to the beginning of the new block 否则,如果其他地方有足够大的空闲块,则分配该块,复制旧块中的数据,释放旧块并返回指向新块开头的指针
  • Else report failure by returning NULL . 否则返回NULL报告失败。

So, you can test for failure by testing for NULL , but be aware that you don't overwrite the old pointer too early: 因此,您可以通过测试NULL来测试失败,但请注意,您不要过早覆盖旧指针:

int* p = malloc(x);
/* ... */
p = realloc(p, y); /* WRONG: Old pointer lost if realloc fails: memory leak! */
/* Correct way: */
{
  int* temp = realloc(p, y);
  if (NULL == temp)
  {
    /* Handle error; p is still valid */
  }
  else
  {
    /* p now possibly points to deallocated memory. Overwrite it with the pointer
       to the new block, to start using that */
    p = temp;
  }
}

realloc will only succeed if it can return a contiguous ("sequential" in your words) block of memory. realloc只有在它能够返回连续的(你的话中的“顺序”)内存块时才会成功。 If no such block exists, it will return NULL . 如果不存在这样的块,则它将返回NULL

From the man page : 手册页

realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. realloc()返回一个指向新分配的内存的指针,该内存适用于任何类型的变量,可能与ptr不同,如果请求失败,则返回NULL。

So in other words, to detect failure, just check whether the result was NULL. 换句话说,要检测失败,只需检查结果是否为NULL。

EDIT: As noted in the comment, if the call fails, the original memory isn't freed. 编辑:如评论中所述,如果调用失败,则不会释放原始内存。

In general, it depends on the implementation. 一般来说,这取决于实施。 On x86(-64) Linux, I believe the standard doug lea malloc algorithm will always allocate a minimum of a standard x86 page (4096 bytes) so for the scenario you described above, it would just reset the boundaries to accomodate the extra bytes. 在x86(-64)Linux上,我相信标准的doug lea malloc算法总是会分配最小的标准x86页面(4096字节),因此对于上面描述的场景,它只会重置边界以容纳额外的字节。 When it comes to, say, reallocating a buffer of 7bytes to PAGE_SIZE+1 I believe it will try to allocate the next contiguous page if available. 比如说,重新分配7bytes的缓冲区到PAGE_SIZE + 1,我相信它会尝试分配下一个连续页面(如果可用)。

Worth reading the following, if you're developing on Linux: 如果你在Linux上开发,请阅读以下内容:

By default, Linux follows an optimistic memory allocation strategy. 默认情况下,Linux遵循乐观的内存分配策略。 This means that when malloc() returns non-NULL there is no guarantee that the memory really is available. 这意味着当malloc()返回非NULL时,无法保证内存确实可用。 This is a really bad bug. 这是一个非常糟糕的错误。 In case it turns out that the system is out of memory, one or more processes will be killed by the infamous OOM killer. 如果事实证明系统内存不足,臭名昭着的OOM杀手就会杀死一个或多个进程。 In case Linux is employed under circumstances where it would be less desirable to suddenly lose some randomly picked processes, and moreover the kernel version is sufficiently recent, one can switch off this overcommitting behavior using a command like: 如果在不太可能突然丢失一些随机选择的进程的情况下使用Linux,而且内核版本足够新,可以使用如下命令关闭这种过度使用的行为:

 # echo 2 > /proc/sys/vm/overcommit_memory 

See also the kernel Documentation directory, files vm/overcommit-accounting and sysctl/vm.txt. 另请参阅内核文档目录,文件vm / overcommit-accounting和sysctl / vm.txt。

FreeBSD和Mac OS X都有reallocf()函数,当无法分配请求的内存时,它将释放传递的指针(参见man realloc)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法查询 realloc 会做什么,或者阻止它复制 Windows 和 Linux 上的所有内存? - Is there a way to either query what would realloc do, or prevent it from copying all memory on Windows and Linux? realloc() 浪费了很多空间,我做错了什么? - realloc() is wasting a lof of space, what did I do wrong? C: realloc 如何处理它释放的内存? - C: what does realloc do with memory that it frees up? 使用realloc扩展使用malloc占用的内存空间? - Using realloc to extend memory space taken with malloc? 如果在使用 realloc 收缩后我仍然可以从另一个指针读取内容,那么 realloc 对释放的 memory 有什么作用? - What does realloc do with the freed memory, if I can still read the content from another pointer after shrinkage with realloc? 我是否需要为指针分配空间以及为内存区域分配空间,其地址将保存在指向指针和重新分配的指针中的指针中 - do I need to allocate space for pointer as well as space for memory area whose address will be kept in pointer in pointer to pointer and realloc 将重新分配对旧指针做什么 - what will realloc do to the old pointer 使用realloc减少数组时内存会发生什么? - What happens with the memory when decrease array with realloc? 使用realloc获得的扩展内存包含了什么? - What are the contains of the extended memory obtained using realloc? 在此代码段中realloc有什么作用? - What does realloc do in this code snippet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM