C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28630
Number of posts: 94612

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
pls hlp me implement the function malloc in c Posted by liyanga on 20 Nov 2009 at 9:34 PM
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.
Report
Re: pls hlp me implement the function malloc in c Posted by liyanga on 20 Nov 2009 at 9:42 PM
and pls d help m as fast as u can cuz iv only 5 days left to complete this
Report
Re: pls hlp me implement the function malloc in c Posted by AsmGuru62 on 21 Nov 2009 at 6:13 AM
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.
Report
Re: pls hlp me implement the function malloc in c Posted by liyanga on 23 Nov 2009 at 6:30 AM
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
Report
Re: pls hlp me implement the function malloc in c Posted by AsmGuru62 on 24 Nov 2009 at 4:45 AM
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()?!..
Report
Re: pls hlp me implement the function malloc in c Posted by Gregry2 on 29 Nov 2009 at 4:51 PM
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...
Report
Re: pls hlp me implement the function malloc in c Posted by liyanga on 29 Nov 2009 at 11:10 PM
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
Report
Re: pls hlp me implement the function malloc in c Posted by liyanga on 4 Dec 2009 at 9:43 PM
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’s required size or for a block which is greater than that size. Once the block is found if it’s free it writes -1 on the first byte to indicate that it’s not free. If the block is exactly the size of what the user wants NewMalloc returns a pointer to the assigned block. If the block is greater than the users size it checks if (size of the block-user size)>7. If the above condition is false the remaining bytes are attached to the block which is given to the user and the pointer is returned as above. However a bigger block is given to the user in this case. This is done as 7 bytes are use as a header in this method. If a new block is to be created the size of it should be greater than 7. If the remaining size is greater than 7 the user required block is given to the user and the remaining part is treated as a new block which means that it too will have a header area to indicate the status of the block and the size. The size of the new block is (size of the block-user size-7).
If by any chance NewMalloc couldn’t find a block which is free and which can be used it will return 0.


NewFree algorithm:
When a block is freed by the user this method simply puts -2 to the 1st byte of the header area of the block to indicate that the block is free.
Afterwards if there are any free blocks beside it, it will merge these blocks and will make it one free block and will update the header area as needed. Because of this there want be any free blocks next to another free block. This is the algorithm used in this method



thankyou very much for ur help.i realy appreciate dat.
Report
Re: pls hlp me implement the function malloc in c Posted by deepaksn on 4 Apr 2010 at 10:49 PM
@ liyanga
please send the code to this email id:
deepak.msrit@gmail.com
thanks in advance..
Report
This post has been deleted. Posted by deepaksn on 4 Apr 2010 at 10:49 PM
This post has been deleted.
Report
Re: pls hlp me implement the function malloc in c Posted by IdoDavid on 12 Nov 2010 at 8:37 AM
hi,

please send me your malloc implementation.

ido4485@gmail.com

thanks,

Ido
Report
Re: pls hlp me implement the function malloc in c Posted by madhu123 on 23 Apr 2011 at 11:58 PM
hi can you please email me the code to crazy_hyper92@yahoo.com
Thanks so much.

Report
Re: pls hlp me implement the function malloc in c Posted by madhu123 on 24 Apr 2011 at 12:01 AM
hi can you please email me the code to crazy_hyper92@yahoo.com
Thanks so much.

Report
Re: pls hlp me implement the function malloc in c Posted by bilderbikkel on 22 Sept 2010 at 9:36 AM
I am with AsmGuru that I don't see the point in writing something like the following:

void * MyMalloc(int sz)
{
  return malloc(sz);
}
Report
Re: pls hlp me implement the function malloc in c Posted by swapnaoe on 28 Sept 2010 at 9:57 AM
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.

http://madison-project.wdfiles.com/local--files/tutorials/The_C_Programming_Language.pdf



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.