小编Bum*_*Bee的帖子

自定义malloc()实现头设计

我试图在C中编写一个自定义分配器用于调试目的(作为练习),我将使用单个链表来使用First Fit算法将自由列表保存在一起.我在下面展示了我想在"空内存节点"中创建的结构.

如何在内存的前几个字节处编写头块(一个特定的联合),我得到(我使用malloc()来初始获得一块内存)以便剩余的字节是空闲的?

这是我正在使用的联盟:

/*Define Header Structure for proper alignment*/
union header {
struct{
    union header* next;
    unsigned size ; /*Make it size_t*/
}s; 
double dummy_align_var;
};

-------------------------------------------------------------------------------
|Next        |Size of  |16Byte| User is concerned only about |16Byte|         |
|Free Memory |Allocated|Header| this portion  of memory      |Footer|Checksum |
|Address     |Block    |Picket| and has no knowledge of rest |Picket|         |
-------------------------------------------------------------------------------
|-------Header---------|      ^Address Returned to user
                              ^------User Requested Size-----^
^-------------Memory Obtained From The Operating System-----------------------^
*/
Run Code Online (Sandbox Code Playgroud)

[编辑]根据提供的建议更改块结构.

c memory malloc memory-management allocation

6
推荐指数
1
解决办法
3534
查看次数

标签 统计

allocation ×1

c ×1

malloc ×1

memory ×1

memory-management ×1