Like Tree16Likes

Create malloc and free function in C?

This is a discussion on Create malloc and free function in C? within the C Programming forums, part of the General Programming Boards category; Hey, I want to know how to go about creating functions like malloc and free in C(without using any other ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    343
    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,288
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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
    21,313
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    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%.

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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.

Page 1 of 4 1234 LastLast
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, 06: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21