Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


pls hlp me implement the function malloc in c

liyangaliyanga Member Posts: 7
i am new to this language and i was asked to implement malloc() in c language. i understand how malloc works but have difficulties in implementation. pls help me out. if can gv m a simple code so dat i can learn more on this.


  • liyangaliyanga Member Posts: 7
    and pls d help m as fast as u can cuz iv only 5 days left to complete this
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]malloc is a system function. What I mean is that it needs to allocate memory from the OS. And every C language has malloc already implemented. I do not see the point to do it.[/color]
  • liyangaliyanga Member Posts: 7
    that is ever we were asked to implement it in c as an assigment:(i find it hard as i am new to this language.pls b kind enough to help me.i have an idea of how 2 d it.but coding it is a problem
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]So, what is your idea? Because any such idea will involve calling malloc() from the system. How will it look? Your own MyMalloc() calling a system malloc()?!..[/color]
  • Gregry2Gregry2 Member Posts: 607
    Okay, perhaps instead of implement you mean use it in an example? Otherwise, we assume you mean create a function that allocates memory dynamically on the heap...IOW, actually create your own malloc() function...
  • liyangaliyanga Member Posts: 7
    thank you very much.i managed to implement the code.well it is a code which does the same thing which malloc does and it can be replaced with my code.if any one is interested i can mail you the far it work fine:)once again thank you
  • liyangaliyanga Member Posts: 7
    this is the simple algoritm jst n case u r interested....

    At the very beginning a char array of size 50000 is declared(50000 bytes). All the memory allocations are done in this area and no local variables are allowed. In this array the last 10 bytes are used to store the variables that are being used while implementing these methods. This area is used for this purpose only.

    Memory is used as blocks in these two methods. Each block has one byte to keep track the status of the block as to the particular block is free or not(-2 is used to indicate that the block is free and -1 is used to indicate that it is not free), at most five bytes to store the size of the block an one byte which is null which is used when reading the size of the block using atoi()

    NewMalloc algorithm:
    At the very first time when the array is not used the first bit of the array is made -2 to indicate that the array is free. Then in the next 5 bytes we put the size of the available block which is (array size-10-7).
    When NewMalloc is called it searches for a block which has the size of user
  • deepaksndeepaksn Member Posts: 4
    @ liyanga
    please send the code to this email id:
    [color=Red][email protected][/color]
    thanks in advance..
  • deepaksndeepaksn Member Posts: 4
    This post has been deleted.
  • bilderbikkelbilderbikkel Member Posts: 754
    I am with AsmGuru that I don't see the point in writing something like the following:

    void * MyMalloc(int sz)
    return malloc(sz);
Sign In or Register to comment.