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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:55:03+00:00 2026-06-05T04:55:03+00:00

I’m working on a program that stores a vital data structure as an unstructured

  • 0

I’m working on a program that stores a vital data structure as an unstructured string with program-defined delimiters (so we need to walk the string and extract the information we need as we go) and we’d like to convert it to a more structured data type.

In essence, this will require a struct with a field describing what kind of data the struct contains and another field that’s a string with the data itself. The length of the string will always be known at allocation time. We’ve determined through testing that doubling the number of allocations required for each of these data types is an unnacceptable cost. Is there any way to allocate the memory for the struct and the std::string contained in the struct in a single allocation? If we were using cstrings I’d just have a char * in the struct and point it to the end of the struct after allocating a block big enough for the struct and string, but we’d prefer std::string if possible.

Most of my experience is with C, so please forgive any C++ ignorance displayed here.

  • 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-05T04:55:06+00:00Added an answer on June 5, 2026 at 4:55 am

    If you have such rigorous memory needs, then you’re going to have to abandon std::string.

    The best alternative is to find or write an implementation of basic_string_ref (a proposal for the next C++ standard library), which is really just a char* coupled with a size. But it has all of the (non-mutating) functions of std::basic_string. Then you use a factory function to allocate the memory you need (your struct size + string data), and then use placement new to initialize the basic_string_ref.

    Of course, you’ll also need a custom deletion function, since you can’t just pass the pointer to “delete”.


    Given the previously linked to implementation of basic_string_ref (and its associated typedefs, string_ref), here’s a factory constructor/destructor, for some type T that needs to have a string on it:

    template<typename T> T *Create(..., const char *theString, size_t lenstr)
    {
      char *memory = new char[sizeof(T) + lenstr + 1];
      memcpy(memory + sizeof(T), theString, lenstr);
    
      try
      {
        return new(memory) T(..., string_ref(theString, lenstr);
      }
      catch(...)
      {
        delete[] memory;
        throw;
      }
    }
    
    template<typename T> T *Create(..., const std::string & theString)
    {
      return Create(..., theString.c_str(), theString.length());
    }
    
    template<typename T> T *Create(..., const string_ref &theString)
    {
      return Create(..., theString.data(), theString.length());
    }
    
    template<typename T> void Destroy(T *pValue)
    {
      pValue->~T();
    
      char *memory = reinterpret_cast<char*>(pValue);
      delete[] memory;
    }
    

    Obviously, you’ll need to fill in the other constructor parameters yourself. And your type’s constructor will need to take a string_ref that refers to the string.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.