Thread: Create malloc and free function in C?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    [](){}(); 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.

  2. #2
    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...

  3. #3
    [](){}(); 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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    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.
    Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...

    Heap Functions (Windows)

    Basically you create a private heap... but it also gives you far better functionality than malloc() and free()... for example you can discover the size of any memory block with HeapSize() and you can confirm HeapFree() with HeapValidate()...

    I've often noticed that people seem naturally prone to go in the wrong direction ... they avoide the OS and it's first level APIs like they're going to die if they use them, when in fact getting closer to the core OS very often reveals increased power and capability.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...

    Heap Functions (Windows)

    Basically you create a private heap... but it also gives you far better functionality than malloc() and free()... for example you can discover the size of any memory block with HeapSize() and you can confirm HeapFree() with HeapValidate()...

    I've often noticed that people seem naturally prone to go in the wrong direction ... they avoide the OS and it's first level APIs like they're going to die if they use them, when in fact getting closer to the core OS very often reveals increased power and capability.
    The Linux kernel api is still a little complicated for me because my unfortunate ignorance of most of the C standard library. I'm working to rectify that.

    Also, I'd be in a bad situation if someday, I need to demonstrate my program to someone coming from another platform.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CommonTater View Post
    Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...

    Heap Functions (Windows)
    It would be quite a bizarre exercise to implement your own malloc() on top of HeapAlloc(), because that's exactly what the MS runtime does already. Not only reinventing the wheel, but reinventing it exactly the same as it already is.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

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