malloc如何分配小于4KB的内存?(How malloc allocates memory less than 4KB?)

编程入门 行业动态 更新时间:2024-10-28 06:35:41
malloc如何分配小于4KB的内存?(How malloc allocates memory less than 4KB?)

如果malloc调用VirtualAlloc()函数来分配内存(分配最小4Kb),malloc如何为int分配4个字节?

If malloc calls VirtualAlloc() function to allocate memory (which allocates minimum 4Kb), how malloc allocates 4 bytes for int?

最满意答案

malloc以多倍的页面大小从操作系统请求内存(很明显,因为页面大小是定义分配内存的量),并以较小的块传递给你。

这与所有的内存分配器并没有什么不同 - 实际上,在后台使用malloc专用内存分配器(例如Boost.Pool )可以再次实现这一点:它们通过malloc分配更大的内存块并将其交给您在较小的部分。

malloc requests memory from the OS in multiples of the page size (obviously, since the page size is by definition the quantum of allocated memory) and hands it out to you in smaller chunks.

That's not different than what all memory allocators do -- in fact, specialized memory allocators (e.g. Boost.Pool) that use malloc behind the scenes do exactly this once more: they allocate a bigger chunk of memory through malloc and hand it out to you in smaller pieces.

更多推荐

本文发布于:2023-08-05 11:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1432701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分配   内存   malloc   KB   memory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!