如果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.
更多推荐
发布评论