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

  • Home
  • SEARCH
  • 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 8422001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:18:44+00:00 2026-06-10T03:18:44+00:00

I’m currently writing an application, which communicates with another application using boost::interprocess . However

  • 0

I’m currently writing an application, which communicates with another application using boost::interprocess. However I have some problems with boost::interprocess::string. Creating a string from a const char* works just as expected, however when I try to create an empty string and later fill it with appropriate content (since I don’t know it when creating the string) it somehow looses the first character. The following example code illustrates this behaviour:

#include <iostream>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>

namespace ip = boost::interprocess;
int main(int ac, char* av[]){
    typedef ip::allocator<char,ip::managed_shared_memory::segment_manager>   IpStringAllocator;
    typedef ip::basic_string<char, std::char_traits<char>,IpStringAllocator> IpString;
    const char* name = "SharedMem";
    ip::shared_memory_object::remove(name);

    ip::managed_shared_memory mem(ip::create_only ,name ,65536);
    auto str  = mem.construct<IpString>("string")(name, mem.get_segment_manager());
    auto str2 = mem.construct<IpString>("string2")("", mem.get_segment_manager());
    *str2     = name;
    std::cout<<*str<<"|"<<*str2<<std::endl;
    //mem.destroy_ptr<IpString>(str);
    //mem.destroy_ptr<IpString>(str2);
    return 0;
}

The output of this application is SharedMem|haredMem, so str holds SharedMem, just as expected. However str2 only contains haredMem, missing the first character of the string.

So why is str2 missing one character and how do I avoid this behaviour?

Another problem I’m having is that trying to destroy the strings using destroy_ptr leads to a segfault (that is what happens when I uncomment the two second to last lines in the code above) when compiling the code with -O3 (but only then it seems). What is the reason for that behaviour and what do I need to do differently to avoid the segfault?

I’m using gcc 4.6.1 (compile flags: -std=c++0x -O3 -g) and boost 1.47 on linux mint inside a virtual machine (virtualBox)

Edit:
As it turns out the assignment works fine when compiling without optimizations, but exhibits the described behaviour when compiling with -O2 or -O3.

Furthermore the assignment works (at least in this contrieved sample code) even with optimization, if I do it twice, making the relevant code look like this:

auto str2 = mem.construct<IpString>("string2")("", mem.get_segment_manager());
*str2     = name;
*str2     = name;
std::cout<<*str<<"|"<<*str2<<std::endl;

While this seems to avoid the problem so far it is hardly a solution I would want to depend on.

  • 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-10T03:18:46+00:00Added an answer on June 10, 2026 at 3:18 am

    Compress information from comments

    • Upgrade compiler. No sure about boost, but I think, what problem in compiler. Anyway, when using g++ 4.8.0(experimental)/4.7.1 and boost 1.51.0 the problem does not occur. If you can’t upgrade your compiler, then
    • Set -O0, -O1 or -Os optimizer flag(testing with g++ 4.6.3 and boost 1.47)

    Also, I read section in g++ man about Optimizer Options. I tried to combine options that disabled by -Os with -O2 flag, but I was not successful. IMO g++ use undocumented optimizer flags.

    • 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 have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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

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.