title: How to use multiple times memcpy to convert back a std::vector<unsigned char>?
body: <p>I want to convert a set of values which are <code>strings</code>, <code>doubles</code>, and <code>time_t</code> to a <code>std::vector&lt;unsigned char&gt;</code> in C++. I am using <code>memcpy</code> for that and calling it every time that I have a new value. I just realized that I have to call it backward to concatenate on the right order on my vector. After I convert all my values I want to convert back. The way I am doing is calling each time <code>memcpy</code> again to get the original value converted back. But because the values after the first are in the middle of the vector I cannot convert it back properly.</p>
  
  <p>How do I convert back all values from the vector separately? My code is below and the output too. Thanks</p>
  
  <pre><code>int main(void) {
      std::string lat = "lat-&gt;";
      double latitude = 13.123456;
  
      std::vector&lt;unsigned char&gt; result(sizeof(lat) + sizeof(latitude));
  
      std::cout &lt;&lt; "copying to the vector" &lt;&lt; std::endl;
      memcpy(result.data(), &amp;latitude, sizeof(latitude)); // add string to the vector
      memcpy(result.data(), &amp;lat, sizeof(result.size()) + sizeof(lat)); // add double to the same vector
      std::cout &lt;&lt; "copied to the vector\n" &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "printing the vector" &lt;&lt; std::endl;
      for (int j = 0; j &lt; result.size(); j++) {
          std::cout &lt;&lt; result[j];
      }
      std::cout &lt;&lt; std::endl;
      std::cout &lt;&lt; "printed the vector\n" &lt;&lt; std::endl;
  
      // testing converting back ...................
      std::cout &lt;&lt; "printing back the original value" &lt;&lt; std::endl;
      double d;
      std::string value;
      // make sure the vector is the right size
      if (result.size() != (sizeof(d) + sizeof(lat)))
          throw std::runtime_error {
                  "Size of data in vector and float do not match" };
      // copy the bytes into the float
      memcpy(&amp;value, result.data(), sizeof(value));
      std::cout &lt;&lt; value;
      memcpy(&amp;d, result.data(), sizeof(value) + sizeof(d));
      std::cout &lt;&lt; d &lt;&lt; std::endl;
      std::cout &lt;&lt; "printed back the original value\n" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>copying to the vector
  copied to the vector
  
  printing the vector
  ��(�lat-&gt;Pş0��(�
  printed the vector
  
  printing back the original value
  lat-&gt;6.95297e-310
  printed back the original value
  
  Segmentation fault (core dumped)
  </code></pre>