Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 167875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:21:11+00:00 2026-05-11T12:21:11+00:00

I want to make my own malloc/free so I can make a precise copying

  • 0

I want to make my own malloc/free so I can make a precise copying allocator.

Any gurus have any tips and suggestions?

I have a few questions for now:

  1. Should I just malloc large chunks of memory, and then distribute from that so I don’t have to call the system calls?
  2. How are copying collectors usually done? I would imagine that this part is a bit complicated to do efficiently. My naive implementation would just malloc a block the size of the remaining objects which would require 2x the space.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:21:12+00:00Added an answer on May 11, 2026 at 12:21 pm

    There’s rather a lot of good literature on implementing malloc and similar things. but I notice that you include C++ here — are you aware that you can write your own implementation of new and delete in C++? That might be useful as a way to do it easily.

    In any case, the characteristics you want are going to depend pretty heavily on your workload, that is, on the pattern of usage over time. If you have only mallocs and new frees, it’s easy, obviously. If you have only mallocs of one, or a few different, block sizes, that’s also simple.

    In other languages, you get some leverage by having the ability to chain memory together, but C isn’t that smart.

    The basic implementation of malloc simply allocates a header that contains the data length, an "in use flag", and the malloced memory. Malloc then constructs a new header at the end of its space, allocates the memory, and returns a pointer. When you free, it just resets the in use flag.

    The trick is that when you do a lot of mallooc and free, you can quickly get a lot of small blobs that aren’t in use, but are hard to allocate. So you need some kind of bumpo gc to merge blocks of memory.

    You could do a more complicated gc, but remember that takes time; you don’t want a free to take up a lot of time.

    There’s a nice paper on Solaris malloc implementations you might find interesting. Here’s another on building an alternative malloc, again in Solaris, but the basics are the same. And you should read the Wikipedia article on garbage collection, and follow it to some of the more formal papers.

    Update

    You know, you really should have a look at generational garbage collectors. The basic idea is that the longer something remains allocated, the more likely is it to stay allocated. This is an extension of the "copying" GC you mention. Basically, you allocate new stuff in one part of your memory pool, call it g0. When you reach a high water mark on that, you look through the allocated blocks and copy the ones that are still in use to another section of memory, call it g1, Then you can just clear the g0 space and start allocating there. Eventually g1 gets to its high water mark and you fix that by clearing g0, and clean up g1 moving stuff to g0, and when you’re done, you rename the old g1 as g0 and vice versa and continue.

    The trick is that in C especially, the handles you hand out to malloc’ed memory are straight raw pointers; you can’t really move things around without some heap big medicine.

    Second update

    In comments, @unknown asks "Wouldn’t moving stuff around just be a memcpy()". And indeed it would. but consider this timeline:

    warning: this is not complete, and untested, just for illustration, for entertainment only, no warranty express or implied

    /* basic environment for illustration*/ void * myMemoryHdl ; unsigned char lotsOfMemory[LOTS]; /* this will be your memory pool*/  

    You mallocate some memory

    /* if we get past this, it succeded */ if((myMemoryHdl = newMalloc(SIZE)) == NULL)     exit(-1); 

    In your implementation of malloc, you create the memory and return a pointer to the buffer.

    unsigned char * nextUnusued = &lotsOfMemory[0]; int partitionSize = (int)(LOTS/2); int hwm = (int) (partition/2); /* So g0 will be the bottom half and g1 the top half to start */ unsigned char * g0 = &lotsOfMemory[0]; unsigned char * g1 = &lotsOfMemory[partitionSize];   void * newMalloc(size_t size){    void * rtn ;    if( /* memory COMPLETELY exhausted */)       return NULL;    /* otherwise */    /* add header at nextUnused */    newHeader(nextUnused);     /* includes some pointers for chaining                                * and a field with values USED or FREE,                                 * set to USED */    nextUnused += HEADERLEN ;  /* this could be niftier */    rtn = nextUnused ;    nextUnused += size ; } 

    Some of the things are freed

      newFree(void * aHandle){      *(aHandle-offset) = FREE ; /* set the flag in the header,                                   * using an offset. */   } 

    So now you do all the stuff and you get to your high water mark.

     for( /* each block in your memory pool */ )     if( /* block header is still marked USED */ ) {         memcpy(/* block into other partition */);     }  /* clear the partition */  bzero(g0, partitionSize); 

    Now, go back to the original handle you saved in myMemHdl. What does it point to? (Answer, you just set it to 0x00 with bzero(3).)

    That’s where the magic comes in. In C at least, the pointer you returned from your malloc is no longer under your control — you can’t move it around after the fact. In C++, with user-defined pointer-like types, you can fix that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you make the .xla file if you want to create your own
I want to make a web service where users can make their own pages.
I have just started looking into msbuild, because I want to make my own
If I want to make my own clickable buttons, I can do this: <a
I want to make my own keyboard. I have the following code When I
I want to make my own Start Menu replacement and I am trying to
Hii, I want to make my own snmp server and agent.with my own MIB
I want to allow users to make their own Python mods for my game,
I want make interactive application where user launches it and can do various task
I have style sheet with a class name changebackgroundcolor i want make change in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.