假设我们对大小为n的内存块执行malloc请求,其中对于k> 0,为2 ^ k!= n。
Malloc返回我们用于该请求的内存块的空间,但是如何从页面处理restig缓冲区。我读到的页面通常是内存的块,是2的幂。

Wiki声明以下内容:

 Like any method of memory allocation, the heap will become fragmented; that is,
 there will be sections of used and unused memory in the allocated
 space on the heap. A good allocator will attempt to find an unused area
 of already allocated memory to use before resorting to expanding the heap.

所以我的问题是如何追踪?

编辑:使用malloc时如何跟踪未使用的内存?

最佳答案

正如Morten Siebuhr早已指出的那样,这实际上取决于具体的实现方式。在非常简单的情况下,可能会有一系列空闲的,固定大小的内存块(可能都具有相同的大小),因此,未使用的内存被简单地浪费掉了。请注意,实际的实现绝不会使用这种简单化的算法。

这是一些简单可能性的概述:http://www.osdcom.info/content/view/31/39/

Wikipedia条目具有几个有趣的链接,包括上面的链接:http://en.wikipedia.org/wiki/Dynamic_memory_allocation#Implementations

最后,使用谷歌搜索“malloc实现”会发现一堆有值(value)的链接(双关语意味深长)。

关于c - 当请求的存储块不是2的幂时会发生什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3316334/

10-16 08:34