1

I'm working with Boost::Serialization library in C++. When I want to restore the object, I use the code below:

// ostr is of type std::ostringstream

std::vector<Certificate *> newCRL;
{
    std::istringstream ifs(ostr.str());
    boost::archive::text_iarchive ia(ifs);
    ia >> newCRL;
}

If the ostr is short in length, I can successfully restore my original object, but the problem I have is with long ostr. If ostr is big, exception below is thrown in run-time:

std::length_error: basic_string::resize

I think std::istringstream ifs(ostr.str()); is the source of this exception. Is there any work around on this, that I can use to reconstruct big objects?

ManiAm
  • 1,759
  • 5
  • 24
  • 43
  • 2
    Why not use a `std::stringstream` rather than having separate `std::istringstream` and `std::ostringstream`s? – ildjarn May 04 '12 at 18:53
  • Is it absolutely necessary for this function to use `stringstream`s at all? Avoid using them as a buffer if you can, since they can take very large amounts of memory. Read directly from the original stream where you can. – Mooing Duck May 04 '12 at 18:59
  • @ildjarn: you are right! I will change my code, but the exception still exist! – ManiAm May 04 '12 at 19:07
  • @MooingDuck: text_iarchive takes an istream as input argument. I will change the type of ostr to stringstream and fed it into the text_iarchive, as ildjam said. – ManiAm May 04 '12 at 19:09
  • @ManiAmoozadeh: No, I mean, why do you have an `ostr` to begin with? Is it absolutely necessary? – Mooing Duck May 04 '12 at 19:11
  • @MooingDuck: aha. I have a very long string and I decided to store it in ostr with type ostringstream! I can use other structures! ostringstream was the first option came to my mind to store my long string! and then I can fed it easilly to text_iarchive! – ManiAm May 04 '12 at 19:15
  • @osgx: at least 600 characters, but it can be much more (around 6000) – ManiAm May 04 '12 at 19:24
  • Doubtful the string size is the problem. Much more likely the issue is with the serialized content. – chill Dec 01 '12 at 14:07

0 Answers0