Thread: Writing my own malloc.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Writing my own malloc.

    Hey guys,
    I'm trying to think of a way to write my own malloc & free functions and I have no idea how to do it.
    I can't use the original malloc & free functions (it's not a memory pool if you thought so!) and as you probably guessed, the APIs (HeapAlloc, etc')

    Any ideas? (I'm not asking for the code, just for your assistant)

    And yeah, I know there's a simple example in the K & R, I haven't seen that one though.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by eXeCuTeR View Post
    Hey guys,
    I'm trying to think of a way to write my own malloc & free functions and I have no idea how to do it.
    I can't use the original malloc & free functions (it's not a memory pool if you thought so!) and as you probably guessed, the APIs (HeapAlloc, etc')

    Any ideas? (I'm not asking for the code, just for your assistant)

    And yeah, I know there's a simple example in the K & R, I haven't seen that one though.
    What OS are you using? I'm assuming Windows since you mentioned HeapAlloc.

    Why can't you use the Windows API functions? That's the only way to safely get memory from the Windows. After that, what you do with the memory is up to you.
    If you don't get the memory from Windows, then Windows could allocate that memory to another program and overwrite your data (although you'd probably get an IPF long before that happens).

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by eXeCuTeR View Post
    I can't use the original malloc & free functions (it's not a memory pool if you thought so!) and as you probably guessed, the APIs (HeapAlloc, etc')
    Then you're screwed, because you can't just pick an address and stick data there without asking the OS for access to that address. And that request, by definition, is done through some "API."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Unless you want to do something like

    char pool[1000000];
    Then write some alloc/free code to chop that up into handy slices.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    As far as I can see, you have 2 remaining choice:

    • Use a big global array
    • Allocate a big piece of the stack at the start of main

    The first solution would be a little bit cleaner than the second, since the second would need that you add some code at the start and end of main.
    I hate real numbers.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use the Windows API.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I have both Windows XP and Debian Lenny (testing) on my computer so im flexible.
    ---
    Again, I can't use the API functions, I've mentioned that, it's supposed to be done by "pure" code (I assume I'm supposed to include only stdio.h)

    EDIT:
    And that request, by definition, is done through some "API."
    Could you give an example to such function?
    Last edited by eXeCuTeR; 07-18-2008 at 04:37 PM.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, you've mentioned you "can't" use the Windows API, but how do you think malloc() actually works? On Windows, it can't work without using it unless you use some work around by using a memory pool which you said you didn't want to do. This is the year 2008. Operating systems rule the systems they are on. If you want to use the system, you must show respect to the OS. It is a king on your system.

    If I was to write an implementation of malloc(), I would have it call HeapAlloc() with GetProcessHeap() as the heap it uses. Depending on how advanced I would want to get, I would also consider trying to create a separate heap (ie. HeapCreate()) and manage all of the heaps together, some for internal malloc() usage, and some for pure user needs.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by foxman View Post
    As far as I can see, you have 2 remaining choice:

    • Use a big global array
    • Allocate a big piece of the stack at the start of main

    The first solution would be a little bit cleaner than the second, since the second would need that you add some code at the start and end of main.
    That sort of defeats the purpose of dynamic allocation, which is to not actually allocate memory until you need it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by brewbuck View Post
    That sort of defeats the purpose of dynamic allocation, which is to not actually allocate memory until you need it.
    I agree with you, but it looks like that's what eXeCuTeR wants.
    I hate real numbers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by foxman View Post
    I agree with you, but it looks like that's what eXeCuTeR wants.
    Nope, what I want is to create my own malloc; a function that does the same as malloc, and not some kind of a function that also creates extra space which might not be in use.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Then you must use the Windows API on Windows if you want the exact behavior as malloc().

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by eXeCuTeR View Post
    Nope, what I want is to create my own malloc; a function that does the same as malloc, and not some kind of a function that also creates extra space which might not be in use.
    But why do you need to create your own malloc()? What are you trying to do that makes you think you need your own malloc()?

    If you don't want to call an OS API, your only other option is to create a big global or static variable and start slicing it up when someone calls your malloc()...

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cpjust View Post
    But why do you need to create your own malloc()? What are you trying to do that makes you think you need your own malloc()?
    Good question. The only reason I can think of is a wish for additional features (eg tracking allocations and deallocations)
    Quote Originally Posted by cpjust View Post
    If you don't want to call an OS API, your only other option is to create a big global or static variable and start slicing it up when someone calls your malloc()...
    Which, if you think about it, is essentially what the OS API functions do, albeit on a grander scale; talk about recreating a wheel.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by cpjust View Post
    But why do you need to create your own malloc()? What are you trying to do that makes you think you need your own malloc()?

    If you don't want to call an OS API, your only other option is to create a big global or static variable and start slicing it up when someone calls your malloc()...
    It's not that I'm gonna use my own malloc now, it's just that I've been given this challenge and I'm trying to solve it now.
    ---
    Well, so, you're saying that using HeapCreate, and HeapAlloc, etc', is the only way? If so, it's pretty stupid to give such challenge which can only be solved using API functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc for extending the size....!
    By patil.vishwa in forum C Programming
    Replies: 5
    Last Post: 09-11-2008, 03:34 AM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. A bug I cannot see. . . malloc issue
    By Kennedy in forum C Programming
    Replies: 16
    Last Post: 09-26-2007, 09:49 AM
  4. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  5. help with malloc
    By geetee in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 12:07 PM