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 8842727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:02:07+00:00 2026-06-14T11:02:07+00:00

is there a way to design a C++ memory pool (for a specific class

  • 0

is there a way to design a C++ memory pool (for a specific class only) that can allocate and free memory in O(1)?

let’s say I have class T, I was thinking of allocating only chunks of 100*sizeof(T) when needed. But then how can I deal with the fragmentation that occurs when a specific object is deleted in the chunk? I can have a boolean value for each slot to tell if the slot is occupied or not, but then I need an algorithm to give me the next free slot.

Is there any standard way to implement this in O(1)? I guess this is a fairly common thing

edit :
image to see what I mean
pic

  • 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. Editorial Team
    Editorial Team
    2026-06-14T11:02:10+00:00Added an answer on June 14, 2026 at 11:02 am

    This solution is using additional memory (which may or may not be what you want), also you’ll have problems if you try to free a chunk twice in a row.

    Preallocate enough memory. Divide it into chunks, one per object. Keep a list of free chunks. When you allocate a new object, pick a chunk from the top of the free chunks list. When you free an object, append its chunk to the list of free chunks. Both of these operations are O(1).

    It would look something like this:

    Init:

    char* mem = new char[CHUNK_SIZE * OBJ_COUNT];
    std::list<char*> free_chunks;
    for (char* c = mem, int i = 0; i < OBJ_COUNT; ++i)
    {
       free_chunks.push_back(c);
       c += CHUNK_SIZE;
    }
    

    Getting a new chunk for allocation:

       if(free_chunks.size() > 0)
       {
        char* c = free_chunks.back();
        free_chunks.pop_back();
        return c;
       }
       else{ // Error, not enough memory... }
    

    Returning a chunk after deallocation:

    free_chunks.push_back(c);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there way that I can read the file from remote server using fopen
Is there way to better identify design pattern in source codes, esp. if you
Is there an easy way to design a website to facilitate an iphone user
Is there way to copy a file into Plone with WebDAV and have Plone
I have a Entity Framework design with a few tables that define a graph.
I have a C++ key/value table that looks like this: class kvBucket { ...
What is a good way to design a class? I'm trying to create a
I am trying to find a way to allocate memory dynamically for memory mapped
I need some design suggestions for an Image class hierarchy. Currently, I have 2
Is there way to get file from windows xp command prompt? I tried to

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.