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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:46:13+00:00 2026-06-04T22:46:13+00:00

I was playing with some code and I wrote this little class to pool

  • 0

I was playing with some code and I wrote this little class to pool std::wstring. As I profile it I’d like to know your oppinion about approaching a pool of strings this simple.

I have a std::vector of std::wstring and two maps, one for holding the indexes of the unused elements in the vector and another for the ones that are being used.

If a string is requested with new_string() and there isn’t any free indexes left, I resize the vector and insert the newly added indexes to the free indexes map.

Here is the code:

typedef std::wstring string_t;

class string_pool
{
    public:
        string_pool()
        {
            grow_factor = 2;
            strings.resize(10);
            for (size_t i=0; i<10; i++)
            {
                free_indexes.insert(std::make_pair(&strings[i],i));
            }
        }

        virtual ~string_pool()
        {

        }

        string_t* new_string()
        {
            if (!free_indexes.size())
            {
                size_t old_size = strings.size();
                size_t new_size = old_size+grow_factor;
                for (size_t i=old_size;i<new_size; i++)
                {
                    free_indexes.insert(std::make_pair(&strings[i],i));
                }
                strings.resize(new_size);
            }
            iter = free_indexes.begin();
            used_indexes.insert(*iter);     // Mark the index as used
            string_t* str = (*iter).first;    // Get a pointer to the string
            free_indexes.erase(iter);       // Erase the free index
            return str;
        }

        void delete_string(string_t* str)
        {
            str->clear();
            iter = used_indexes.find(str);
            free_indexes.insert(*iter);     // Mark the index as free
            used_indexes.erase(iter);       // Erase the used index
        }

    protected:
        std::map<string_t*, size_t>             used_indexes;
        std::map<string_t*, size_t>             free_indexes;
        std::vector<string_t>                   strings;
        size_t                                  grow_factor;
        std::map<string_t*, size_t>::iterator   iter;
};
  • 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-04T22:46:15+00:00Added an answer on June 4, 2026 at 10:46 pm

    The new/delete you care about is not allocation of the string descriptors, but the string data. Pooling string objects doesn’t help much, the content will still be dynamically allocated.

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

Sidebar

Related Questions

I am playing around with ctypes a little bit to massage some C code
I have been playing around with some debugging and wrote some C code that
I wrote some C# code and in it I initiate a class by placing
I have some code that looks like this: from pyparsing import Word, alphas, Optional,
I have written some code for playing a .wav through my application. Now I
Here is some C++ code I'm playing around with: #include <iostream> #include <vector> #define
I have written some WebGL code, actually I am playing with the examples that
Does anyone have some useful beginner tutorials and code snippets for playing with basic
I'm a little confused as to what's going on, i'm playing with some programs
I'm looking for some style advice for testing this piece of (Objective-)C++ code, I

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.