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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:57:27+00:00 2026-05-15T06:57:27+00:00

I have a problem using boost serialization using binary archives. It works when using

  • 0

I have a problem using boost serialization using binary archives. It works when using a file stream but I want to store it in my local variable and ultimately save/load it to/from berkeley db.
When executing the program I get a boost::archive::archive_exception: ‘stream error’ when instantiating the binary_iarchive.

#include <sys/time.h>
#include <string>
#include <boost/serialization/serialization.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <fstream>
#include <sstream>

namespace boost {
namespace serialization {

template<class Archive>
void serialize(Archive & ar, timeval & t, const unsigned int version)
{
    ar & t.tv_sec;
    ar & t.tv_usec;
}

}//namespace serialization
}//namespace boost


int main(int, char**)
{
    timeval t1;
    gettimeofday(&t1, NULL);
    char buf[256];

    std::stringstream os(std::ios_base::binary| std::ios_base::out| std::ios_base::in);
    {
        boost::archive::binary_oarchive oa(os, boost::archive::no_header);
        oa << t1;
    }

    memcpy(buf, os.str().data(), os.str().length());
    if(memcmp(buf, os.str().data(), os.str().length()) != 0)
        printf("memcpy error\n");

    timeval t2;
    {
        std::stringstream is(buf, std::ios_base::binary| std::ios_base::out| std::ios_base::in);

        boost::archive::binary_iarchive ia(is, boost::archive::no_header);
        ia >> t2;
    }

    printf("Old(%d.%d) vs New(%d.%d)\n", t1.tv_sec, t1.tv_usec, t2.tv_sec, t2.tv_usec);

    return 0;
}

It works when initializing is with os.str(), so I guess my way of copying the data to my buffer or to is is wrong.

  • 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-05-15T06:57:28+00:00Added an answer on May 15, 2026 at 6:57 am

    Well, for one thing .data() doesn’t have a terminal \0. It’s not a c-string. I didn’t even realize stringstream had a char* constructor (who in their right mind uses them anymore?) but apparently it does and I’d bet it expects \0.

    Why are you trying to do it that way anyway? You’re much better off working in C++ strings. Initialize is with os.str().

    Edit: binary data contains lots of \0 characters and the std::string(char*) constructor stops at the first one. Your deserialization routine will then inevitably try to read past the end of the stream (because it isn’t complete). Use the iterator constructor for std::string when you pass buf into the stringstream.

    std::stringstream is(std::string(buf, buf+os.str().length()), flags);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can keep that idea but add a list or… May 15, 2026 at 11:37 am
  • Editorial Team
    Editorial Team added an answer Just above that example, it says if e is an… May 15, 2026 at 11:37 am
  • Editorial Team
    Editorial Team added an answer Google Chrome extensions are zip files with a different extension… May 15, 2026 at 11:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.