title: Boost binary serialization - double array of fixed length error
body: <p>I'm creating a class that stores lots of doubles for saving/loading with boost. The load needs to be as fast as possible, so getting the binary format to work is the goal.</p>
  
  <p>Basically, I have a class that stores a vector of structs:</p>
  
  <pre><code>vector&lt;DataChunk&gt;
  </code></pre>
  
  <p>where DataChunk stores a double array of fixed length</p>
  
  <pre><code>double data[2048]
  </code></pre>
  
  <p>When I test the functionality with using a text archive (text_iarchive), everything works great. However, when using the binary archive, I get memory access violation on deserializing the class.</p>
  
  <p>More bizarrely, if I fill the double array with the same double value (i.e all elements of data[2048] are equal to 12345), it works. Varying double values seems to crash it, though (see below).</p>
  
  <p>Here's my RawData class:</p>
  
  <pre><code>#pragma once
  #include &lt;boost\serialization\vector.hpp&gt;
  #include &lt;boost\serialization\array.hpp&gt;
  
  using namespace std;
  
  struct DataChunk
  {
  public:
      double data[2048]; //THIS IS THE PROBLEM AREA
      int end;
  private:
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive&amp; ar, const unsigned int version)
      {
          ar &amp; data;
          ar &amp; end;
      }
  };
  
  
  class RawData
  {
  private:
      vector&lt;DataChunk&gt; chunks;
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive&amp; ar, const unsigned int version)
      {
          ar &amp; chunks;
      }
  public:
      void add_chunk(DataChunk chunk){chunks.push_back(chunk);};
      vector&lt;DataChunk&gt; get_chunks(){return chunks;};
      static void save(RawData rd, string path);
      static bool load(RawData &amp; rd, string path);
      void add_raw_data(vector&lt;double&gt; raw_data);
      vector&lt;double&gt; combine_chunks();
  };
  </code></pre>
  
  <p>My save and load functions look like this:</p>
  
  <pre><code>void RawData::save(RawData rd, string path)
  {
      std::ofstream file(path); 
      if(file.good())
      {
  
          boost::archive::binary_oarchive oa(file, std::ios::binary);
          //boost::archive::text_oarchive oa(file);
          oa &lt;&lt; rd;
      }
      file.flush();
      file.close();
  }
  
  bool RawData::load(RawData &amp; rd, string path)
  {
      std::ifstream file(path);
      if(file.good())
      {
  
          boost::archive::binary_iarchive ia(file, std::ios::binary); 
          //boost::archive::text_iarchive ia(file);
          ia &gt;&gt; rd;
          file.close();
          return true;
      }
      else
          return false;
  }
  </code></pre>
  
  <p>In my main function, I test it like this:</p>
  
  <pre><code>string path = "test.data";
  RawData old_data;
  vector&lt;double&gt; raw_data;
  for(int i = 0; i &lt; 5000; i++)
      raw_data.push_back(i * 2048); //change this to constant value and it works...
  old_data.add_raw_data(raw_data);
  
  //serialize
  RawData::save(old_data, path);
  
  //deserialize
  RawData new_data;
  RawData::load(new_data, path);
  
  //grab the chunks and test the values
  vector&lt;DataChunk&gt; chunks_in = new_data.get_chunks();
  for(int i = 0; i &lt; chunks_in.size(); i++)
      for(int j = 0; j &lt; chunks_in[i].end; j++)
          cout&lt;&lt;chunks_in[i].data[j]&lt;&lt;", ";
  return 0;
  </code></pre>