[英]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
在幕后工作大致如下:
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
。
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.