pls hlp me implement the function malloc in c

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.


  • and pls d help m as fast as u can cuz iv only 5 days left to complete this
  • [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]
  • 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
  • [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]
  • 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...
  • 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
  • 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
  • @ liyanga
    please send the code to this email id:
    thanks in advance..
  • This post has been deleted.
  • I am with AsmGuru that I don't see the point in writing something like the following:

    void * MyMalloc(int sz)
    return malloc(sz);
  • My advice is please go through Storage Allocator example in C programming language by Kernighan and Dennis Ritchie. It has malloc implementation in C which can be used on a unix based system. Its just about the system call sbrk for which you may have to an equivalence on the OS you want to implement.

    Link below is the e-book i was referring to.
  • hi,

    please send me your malloc implementation.


  • hi can you please email me the code to
    Thanks so much.

  • hi can you please email me the code to
    Thanks so much.

Sign In or Register to comment.

Howdy, Stranger!

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