malloc如何详细工作? [英] how does malloc work in details?

查看:54
本文介绍了malloc如何详细工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一些关于 malloc 函数的有用信息.当我调用此函数时,它会动态分配内存.它将指针(例如地址)返回到已分配内存的开头.问题:

I am trying to find some useful information on the malloc function. when I call this function it allocates memory dynamically. it returns the pointer (e.g. the address) to the beginning of the allocated memory. the questions:

  • 如何使用返回的地址来读取/写入分配的存储块(使用inderect寻址寄存器还是如何?)

  • how the returned address is used in order to read/write into the allocated memory block (using inderect addressing registers or how?)

如果不可能分配一个内存块,则返回NULL.就硬件而言,什么是NULL?

if it is not possible to allocate a block of memory it returns NULL. what is NULL in terms of hardware?

为了在堆中分配内存,我们需要知道哪些内存部分被占用.该信息(有关占用的内存)的存储位置(例如,如果我们使用小型risc微控制器)?

in order to allocate memory in heap we need to know which memory parts are occupied. where this information (about the occupied memory) is stored (if for example we use a small risc microcontroller)?

推荐答案

Q3管理堆的通常方法是通过链表.在最简单的情况下,malloc函数将保留指向堆中第一个可用空间块的指针,并且每个可用空间块都有一个头,该头指向堆中的下一个可用空间块.因此,就知道没有被占用的内容(并据此推断被占用的内容)而言,堆是有效的自定义.这样可以最大程度地减少管理堆所需的开销RAM.

Q3 The usual way that heaps are managed are through a linked list. In the simplest case, the malloc function retains a pointer to the first free-space block in the heap, and each free-space block has a header that points to the next free space block in the heap. So the heap is in-effect self-defining in terms of knowing what is not occupied (and by inference what is therefore occupied); this minimizes the amount of overhead RAM needed to manage the heap.

当通过malloc调用需要新空间时,通过遍历链表会发现足够大的可用空间块.将找到的可用空间块提供给malloc调用者(带有一个小的隐藏头),如果需要,将一个较小的可用空间块插入到链表中,并在原始可用空间块和内存之间留出任何剩余空间.malloc 调用要求.

When new space is needed via a malloc call, a large enough free-space block is found by traversing the linked list. That found free-space block is given to the malloc caller (with a small hidden header), and if needed a smaller free-space block is inserted into the linked list with any residual space between the original free space block and how much memory the malloc call asked for.

当应用程序释放一个堆块时,它的块仅用链表头格式化,然后添加到链表中,通常使用一些额外的逻辑将连续的自由空间块组合成一个更大的自由空间阻止.

When a heap block is released by the application, its block is just formatted with the linked-list header, and added to the linked list, usually with some extra logic to combine consecutive free-space blocks into one larger free-space block.

malloc的调试版本通常会做更多的事情,包括保留分配区域的链接列表,分配堆区域周围的保护区"以帮助检测内存溢出等.这些会占用额外的堆空间(有效地堆化)在应用程序可用空间方面较小),但是在调试时非常有帮助.

Debugging versions of malloc usually do more, including retaining linked-lists of the allocated areas too, "guard zones" around the allocated heap areas to help detect memory overflows, etc. These take up extra heap space (making the heap effectively smaller in terms of usable space for the applications), but are extremely helpful when debugging.

Q2 NULL指针实际上只是一个零,如果使用该指针,它将尝试访问RAM位置0(几乎始终是OS的保留内存)开始的内存.这是导致大量内存冲突中止的原因,所有这些都是由于程序员缺乏对分配内存的函数的NULL返回进行错误检查而导致的.

Q2 A NULL pointer is effectively just a zero, which if used attempts to access memory starting at location 0 of RAM, which is almost always reserved memory of the OS. This is the cause of a significant quantity of memory violation aborts, all caused by programmer's lack of error checking for NULL returns from functions that allocate memory).

由于永远都不需要非OS应用程序访问存储位置0,因此大多数硬件都会中止非OS软件访问存储位置0的任何尝试.即使使用页面映射,也不会将应用程序内存空间(包括位置0)映射到实际RAM位置0,因为NULL始终为零,但大多数CPU仍会中止尝试访问位置0的假设(假设这是通过a进行的访问).包含NULL的指针.

Because accessing memory location 0 by a non-OS application is never what is wanted, most hardware aborts any attempt to access location 0 by non-OS software. Even with page mapping such that the applications memory space (including location 0) is never mapped to real RAM location 0, since NULL is always zero, most CPUs will still abort attempts to access location 0 on the assumption that this is an access via a pointer that contains NULL.

鉴于您的RISC处理器,您将需要阅读其文档以查看其如何处理尝试访问内存位置0的尝试.

Given your RISC processor, you will need to read its documentation to see how it handles attempts to access memory location 0.

Q1有许多高级语言方法来使用分配的内存,主要是通过指针,字符串和数组.

Q1 There are many high-level language ways to use allocated memory, primarily through pointers, strings, and arrays.

就汇编语言和硬件本身而言,分配的堆块地址只会放入用于内存间接寻址的寄存器中.您将需要查看RISC处理器中的处理方式.但是,如果您使用C或C ++或此类高级语言,则无需担心寄存器.编译器会处理所有这些.

In terms of assembly language and the hardware itself, the allocated heap block address just gets put into a register that is being used for memory indirection. You will need to see how that is handled in the RISC processor. However if you use C or C++ or such higher level language, then you don't need to worry about registers; the compiler handles all that.

这篇关于malloc如何详细工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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