Thread: Create malloc and free function in C?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    10

    Lightbulb Create malloc and free function in C?

    Hey,
    I want to know how to go about creating functions like malloc and free in C(without using any other libraries)? I need to make my own set of memory management functions.

    Thanks...

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I tried this a few days ago.
    What I did was to get a chunk of memory from the os (that would require other functions), and give the new function..say memalloc the liberty to allocate from that region.
    Is that what you wanted to do?
    If not, (for the first part), I don't know of any portable way of doing so.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by guestcheap View Post
    Hey,
    I want to know how to go about creating functions like malloc and free in C(without using any other libraries)? I need to make my own set of memory management functions.

    Thanks...
    Won't happen.... You will never implement OS specific behavior without using the underlying OS libraries.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    I tried this a few days ago.
    What I did was to get a chunk of memory from the os (that would require other functions), and give the new function..say memalloc the liberty to allocate from that region.
    Is that what you wanted to do?
    If not, (for the first part), I don't know of any portable way of doing so.
    And exactly how did you inform the C libarary and application calls of this new memory manager?
    Unless you're about rewriting the entire library, it's better, safer and probably faster to use malloc() and free() as provided.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    See A Memory Allocator for an example.

    And it's easy to replace the functions provided by compiler or OS libraries - just link in an object file with the correct function definition that you've written and it will be used instead of the default ones. Not that I'd recommend it except for educational purposes.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The moment you step away from malloc, you're in the realm of needing to use OS specific options, so you might want to specify your OS.
    GlobalAlloc, HeapAlloc, and VirtualAlloc are other options on Windows.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    And exactly how did you inform the C libarary and application calls of this new memory manager?
    Unless you're about rewriting the entire library, it's better, safer and probably faster to use malloc() and free() as provided.
    Of course it is better and safer to use malloc and free, I just made it for a (sort of) virtual machine I was trying to make.
    It wasn't actually a new memory manager.
    I still used malloc to get the large chunk from the system.
    After that I made a huge char array from it, and gave the sizeof(required) amount from it to the required object by casting it to a void*.
    I read that this process(except some small details) was an old tradition in unix programming before the advent of the standards.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by guestcheap View Post
    I need to make my own set of memory management functions.
    I'd just like to point out that that statement is false. You never 'need' to do such a thing. You might 'want' to do such a thing, which is your own foolish choice, but you never 'need' to do such a thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    I'd just like to point out that that statement is false. You never 'need' to do such a thing. You might 'want' to do such a thing, which is your own foolish choice, but you never 'need' to do such a thing.
    Of course guestcheap needs to do that.
    Quote Originally Posted by Ken Thompson
    You can't trust code that you did not totally create yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Unfortunately, it's also a fact of life that most people can't trust code that they did totally create themselves.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    hey as far of my knowledge,if you want to create such fnctns then it's better to read few in functions.......

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by krishnaswathi View Post
    hey as far of my knowledge,if you want to create such fnctns then it's better to read few in functions.......
    Don't talk with your mouth full and/or your mind away for a vacation.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    Of course it is better and safer to use malloc and free, I just made it for a (sort of) virtual machine I was trying to make.
    It wasn't actually a new memory manager.
    I still used malloc to get the large chunk from the system.
    After that I made a huge char array from it, and gave the sizeof(required) amount from it to the required object by casting it to a void*.
    I read that this process(except some small details) was an old tradition in unix programming before the advent of the standards.
    Ahhh... ok, I see how you did it now... interesting experiment but not very practical.

    malloc() and free() are already abstractions from the underlying OS allocators... So you end up with abstraction of an abstraction...

  14. #14
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    Ahhh... ok, I see how you did it now... interesting experiment but not very practical.

    malloc() and free() are already abstractions from the underlying OS allocators... So you end up with abstraction of an abstraction...
    That was actually my goal; to provide an abstraction for allocating memory to objects from the memory owned by the program(there, the virtual machine).
    Is that possible with malloc?
    First I tried it with realloc but couldn't get the address of the remaining pool(if not returned to the system) after it was done.

    I thought that is what 'guestcheap' had in mind; since the other alternative wasn't very viable.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Thanks for the replies...
    I got some help from Writing a memory manager - OSDev Wiki. I want to this but I have absolutely no idea how. Any help?

    PS:
    I'd just like to point out that that statement is false. You never 'need' to do such a thing. You might 'want' to do such a thing, which is your own foolish choice, but you never 'need' to do such a thing.
    You know, some people actually 'need' to do things. Of course, I don't personally know any of them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc/free help
    By stan4d2012 in forum C Programming
    Replies: 3
    Last Post: 04-13-2009, 07:04 PM
  2. malloc() and free()
    By someprogr in forum C Programming
    Replies: 1
    Last Post: 12-28-2007, 07:16 PM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. need help with malloc-free
    By sleith in forum C Programming
    Replies: 4
    Last Post: 08-22-2007, 08:07 PM
  5. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM

Tags for this Thread