简体   繁体   English

malloc 和堆:用于存储大小和链表信息的额外内存?

[英]malloc and heap: extra memory for storing the size and linked list information?

I have a simple question about the heap and malloc :我有一个关于heapmalloc的简单问题:

When we allocate some memory space using malloc as following:当我们使用malloc分配一些内存空间时,如下所示:

int *p;
p = (int*) malloc (10*sizeof(int));

It actually allocates 10 words in the heap.它实际上在堆中分配了 10 个字。 However, my question is:但是,我的问题是:

The actual memory space used is really 10 words?实际使用的内存空间真的是10个字吗?

Or there are other extra space needed for storing the value of memory size?或者还有其他额外的空间来存储内存大小的值?

Or, even, because the heap is structured as Linked list , is there other memory space being used for storing the address which points to the next node of the list in the heap?或者,甚至,因为堆的结构是链表,是否有其他内存空间用于存储指向堆中列表下一个节点的地址?

It is completely implementation dependent.它完全依赖于实现。

a) It could have a few bytes before each allocated node which contains the size of the node, pointer to the next node, and maybe a previous node pointer and the type of node. a) 在每个分配的节点之前可能有几个字节,其中包含节点的大小、指向下一个节点的指针,以及可能是前一个节点指针和节点的类型。

b) The returned item may have nothing else around it except other allocations. b) 除了其他分配之外,返回的项目可能没有其他任何东西。 A structure elsewhere keeps track of what is allocated and what is free, perhaps by a bitmap or a miniature parallel list.其他地方的结构可能通过位图或微型并行列表跟踪已分配的内容和空闲的内容。

c) Another variation provides several arrays of fixed sized chunks. c) 另一种变体提供了几个固定大小块的数组。 One array may provide 32-byte blocks;一个数组可以提供 32 字节的块; another 128-byte blocks, etc. A bitmap for each array manages allocations.另一个 128 字节的块等。每个数组的位图管理分配。

d) The most minimal implementation I have seen ignores free() completely (that is, free() is a no op) and allocates the next piece of the pool at each malloc() . d) 我见过的最简单的实现完全忽略了free() (也就是说, free()是一个无操作)并在每个malloc()分配池的下一部分。


By far, the most commonly used modern technique is a .到目前为止,最常用的现代技术是一个 Variant b is used in many filesystems like NTFS and FAT.变体b用于许多文件系统,如 NTFS 和 FAT。 Option c was/is favored in many DEC operating systems, especially for kernel use.选项c在许多 DEC 操作系统中曾经/受到青睐,尤其是对于内核使用。 Option d is used by a few minimalist embedded environments with a suitable caveat.选项d被一些带有适当警告的极简嵌入式环境使用。

In most implementations, the requested allocation is rounded up to some natural multiple (usually of 2, 8, 16, etc.) convenient for the algorithm.在大多数实现中,请求的分配被四舍五入到算法方便的某个自然倍数(通常是 2、8、16 等)。 So a series of allocations of 5, 3, 8, 7, 4, 1, and 15 might each be regarded as a 16 byte request.因此,一系列 5、3、8、7、4、1 和 15 的分配可能每个都被视为一个 16 字节的请求。

Memory allocation is dependent on the compiler libraries and the operating system.内存分配取决于编译器库和操作系统。

Both languages don't state a maximum amount of memory that can be allocated.两种语言都没有规定可以分配的最大内存量。 All you are guaranteed is the requested size.您所保证的只是所要求的尺寸。

So if there is any additional memory allocated, it would be platform dependent.因此,如果分配了任何额外的内存,它将取决于平台。

Also, there may be less overhead when allocating larger spaces.此外,分配更大的空间时可能会有更少的开销。

Try writing your own memory allocator and see what is needed, especially when desposing the memory.尝试编写自己的内存分配器,看看需要什么,尤其是在处理内存时。

Yes, it is possible for an implementation of malloc to actually allocate a little more memory than what you requested, store the size of the allocated memory at the start of the allocated memory, then give you a pointer to the immediate next memory address.是的, malloc的实现可能实际分配的内存比您请求的多一点,在分配的内存的开头存储分配的内存的大小,然后给您一个指向下一个内存地址的指针。 When you call free on that pointer, the allocator will go back a little, read the size of the buffer, and figure out how much it needs to actually release.当您对该指针调用free时,分配器将返回一点,读取缓冲区的大小,并计算出实际需要释放多少。

But of course, another possible implementation could keep a list, or a dictionary, or do something entirely different under the hood, as long as it gives you the same specified behavior.但是当然,另一种可能的实现可以保留一个列表或一个字典,或者在幕后做一些完全不同的事情,只要它给你相同的指定行为。

When you allocate memory using malloc , all you get is a pointer to the first address in that memory and a guarantee that so many bytes have been allocated for your use.当您使用malloc分配内存时,您得到的只是一个指向该内存中第一个地址的指针,并保证分配了这么多字节供您使用。 The details of how that memory is allocated and tracked are platform-dependent, and there is no way for you to access that information from within the program.如何分配和跟踪内存的详细信息取决于平台,您无法从程序内部访问该信息。 So while extra memory may be allocated for overhead purposes, you can't use that knowledge in a cross-platform way.因此,虽然可能会出于开销目的分配额外的内存,但您不能以跨平台的方式使用该知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM