如何用C编写一个简单的malloc函数 [英] How to write a simple malloc function in c

查看:118
本文介绍了如何用C编写一个简单的malloc函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为操作系统的一个任务,我们必须写我们自己的code的malloc和free在C编程语言,我知道如果我问code它没有一点我的学习。我面对的不知道从哪里包括初始化字符数组50000字节,使两个列表自由使用的问题。在我的功能,我无法触发的malloc或免费的自动发生。和第三方主程序将被用来测试我的功能.....
如果我的文件是mymalloc.c或什么都

As an assignment in operating systems we have to write our own code for malloc and free in C programming language, I know if i asked the code for it there is no point of me to study. i'm facing the problem of not knowing where to include initializing char array with 50000 bytes and making two lists free and used. in my function i can't trigger malloc or free to happen automatically. and a 3rd party main program will be used to test my functions..... if my file is mymalloc.c or what ever

    void* myalloc(size_t size)
    {
        //code for allocating memory
    }
    void myfree(void *ptr)
    {
        //code for free the memory
    }

在这里发起的内存空间,并列出会做code ..

where do the code for initiating memory space and lists will go..

推荐答案

我觉得你只需要实现一个内存管理器。所以,你不必使用 BRK SBRK ,...
只要把使用的内存在一个简单的数组,在某种程度上其分解。由于它的功课,你想让它尽可能的简单,否则你碰上由于你的任务的复杂性/时间限制的问题。

I think you only have to implement a memory manager. So you don't have to use brk, sbrk, ... Just put used memory in a simple array and fragment it somehow. Since it's homework you want to make it as simple as possible or else you run into problems due to complexity/time constraints of your assignment.

您只需要决定你要使用的战术。我建议使用伙伴系统。虽然它比一个最简单的人有点复杂..也许固定大小的碎片更简单。

You only have to decide which tactic you want to use. I'd suggest to use the buddy system. Though it's a bit more complicated than the most simple ones.. maybe fixed sized fragmentation is simpler..

这也是很好看的。

不要做一些低层次的在其他的答案建议。

Don't do something low-level as suggested in the other answers..

这篇关于如何用C编写一个简单的malloc函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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