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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:05:51+00:00 2026-06-07T11:05:51+00:00

I have a couple different objects that allocate certain objects to communicate with each

  • 0

I have a couple different objects that allocate certain objects to communicate with each other and I decided to give them each a locally managed memory pool where a node can be tagged as free merely by setting a flag within it.

The idea is like this:

struct cat {/**couple data fields*/};

struct rat
{
    rat() : poolIndex_(0), poolSize_(INITIAL_SIZE), pool_(new cat *[poolSize_])
        {
            for (size_t i = 0; i < poolSize_; ++i)
                pool_[i] = new cat;
        }

    size_t poolIndex_;
    size_t poolSize_;
    cat** pool_;
};

I provide a non-member function for rat and his friends to resize their pool whenever they run out of free cat nodes (giving out the nodes is done via poolIndex_++ % poolSize_;). The non-member function is as follows:

void quadruplePool(cat*** pool, size_t& poolIndex, size_t& poolSize)
{
    poolIndex = poolSize;
    cat** tmp = new cat *[poolSize*4];
    for (size_t i = 0; i < poolSize; ++i)
        tmp[i] = (*pool)[i];
    delete[] (*pool);
    (*pool) = tmp;
    poolSize = poolSize*4;
    for (size_t i = poolIndex; i < poolSize; ++i)
        (*pool)[i] = new cat;
}

is there something in the code which could give me a speedup from what I have right now? (Speed is by far critical for me)

  • 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-07T11:05:53+00:00Added an answer on June 7, 2026 at 11:05 am

    I think it is far more efficient to do an array allocation of cats, and when they are freed, place them in a free list.

    struct CatPool {
        size_t poolSize_;
        size_t i_;
        cat *pool_;
        cat *free_;
        typedef std::unique_ptr<cat[]> PoolPtr;
        std::list<PoolPtr> cleanup_;
    
        CatPool (size_t pool_size) : poolSize_(pool_size), free_(0) { grow(); }
    
        void grow () {
            i_ = 0;
            pool_ = new cat[poolSize_];
            cleanup_.push_back(PoolPtr(pool_));
        }
    
        cat * get () {
            cat *c = free_;
            if (c) {
                free_ = free_->next_;
                return c;
            }
            for (;;) {
                if (i_ < poolSize_) return &pool_[i_++];
                grow();
            }
        }
    
        void put (cat *c) {
            c->next_ = free_;
            free_ = c;
        }
    };
    

    But, you should consider using an existing pool allocator implementation. For example, Boost’s object_pool.

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

Sidebar

Related Questions

I have a couple of classes that want to pass each other some information
We have couple different web-apps that share the same db link. The hibernate layer
I have a couple different apps that write text files to the Documents Directory
I have a application that generates a couple of different mails. These mails are
I have some code in a couple of different functions that looks something like
I have a PHP page that does a couple of different things depending on
I'm structuring a database, and found that I have two different objects I'm trying
I have a WCF REST service that exposes a couple dozen objects and based
I have a couple JObjects that are being returned from different places but that
I have a worked on a couple different projects and I have seen two

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.