实际上,`realloc`在后台如何工作? [英] How `realloc` work actually in the background?

查看:94
本文介绍了实际上,`realloc`在后台如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

realloc如何在后台实际工作? 如果在旧地方没有足够的可用内存, 这个分配两个/许多内存块和一个指针 指向那个和其他都与每个内部连接 将其他区域或旧区域复制到足够的新位置 内存可用,指针正在更新到新地址并删除旧内存吗?

How realloc work actually in the background? If there is not enough memory available at old place does this one allocating two/many memory blocks and one pointer pointing to that and other are internally linked with each other or the old region copied into new place where enough memory is available and pointer is updating to new address and deleting the old memory?

realloc依赖于编译器/操作系统还是独立于?

推荐答案

realloc如果堆上后面有足够的可用内存,则尝试扩展可用内存范围.如果不是,则相当于malloc新大小的块,memcpy您的内容在那里,free旧块.这与操作系统和编译器均无关,并且取决于链接的libc的实现.

realloc attempts to extend your available memory range if sufficient memory is available behind it on the heap. If not then it is equivalent to malloc a block of the new size, memcpy your contents there, free the old block. This is independent of both OS and compiler and depends on the implementation of libc that you link against.

类似的说明:mremap/MREMAP_MAYMOVE(在现代Linux上可用)将尝试将虚拟映射扩展为请求的大小.如果那不可能,那么它将把您的映射移动到一个新的虚拟地址,该地址后面有足够的VM空间,然后扩展您的映射.如果您由于没有进行物理复制而经常调整大型映射的大小,那么这将非常快.

On a similar note: mremap/MREMAP_MAYMOVE (available on modern Linux) will attempt to extend your virtual mapping by the requested size. If that is not possible then it will move your mapping to a new virtual address that has sufficient VM space behind it and then extend your mapping. This is very fast if you are frequently resizing large mappings since no physical copying is done.

这篇关于实际上,`realloc`在后台如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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