Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

pls hlp me implement the function malloc in c

liyangaliyanga Posts: 7Member
hello!
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.
«1

Comments

  • liyangaliyanga Posts: 7Member
    and pls d help m as fast as u can cuz iv only 5 days left to complete this
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [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 Posts: 7Member
    that is true.how 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 Posts: 6,519Member
    [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 Posts: 607Member
    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 Posts: 7Member
    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 code.so far it work fine:)once again thank you
  • liyangaliyanga Posts: 7Member
    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 Posts: 4Member
    @ liyanga
    please send the code to this email id:
    [color=Red]deepak.msrit@gmail.com[/color]
    thanks in advance..
  • deepaksndeepaksn Posts: 4Member
    This post has been deleted.
  • bilderbikkelbilderbikkel Posts: 754Member
    I am with AsmGuru that I don't see the point in writing something like the following:

    [code]
    void * MyMalloc(int sz)
    {
    return malloc(sz);
    }
    [/code]
«1
Sign In or Register to comment.