title: Why does memcpy to int not work after calling memcpy to bool value
body: <p>I was playing around with memcpy when I stumbled on a strange result, where a memcpy that is called on the same pointer of memory after bool memcpy gives unexpected result.</p>
  
  <p>I created a simple test struct that has a bunch of different type variables. I cast the struct into unsigned char pointer and then using memcpy I copy data from that pointer into separate variables. I tried playing around the offset of memcpy and shifting the int memcpy before bool (changed the layout of test struct so that the int would go before the bool too). Suprisingly the shifting fixed the problem.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Simple struct containing 3 floats
  struct vector
  {
      float x;
      float y;
      float z;
  };
  
  // My test struct
  struct test2
  {
      float a;
      vector b;
      bool c;
      int d;
  };
  
  int main()
  {
      // I create my structure on the heap here and assign values
      test2* test2ptr = new test2();
      test2ptr-&gt;a = 50;
      test2ptr-&gt;b.x = 100;
      test2ptr-&gt;b.y = 101;
      test2ptr-&gt;b.z = 102;
      test2ptr-&gt;c = true;
      test2ptr-&gt;d = 5;
  
      // Then turn the struct into an array of single bytes
      unsigned char* data = (unsigned char*)test2ptr;
      // Variable for keeping track of the offset
      unsigned int offset = 0;
  
      // Variables that I want the memory copied into they
      float a;
      vector b;
      bool c;
      int d;
  
      // I copy the memory here in the same order as it is defined in the struct
      std::memcpy(&amp;a, data, sizeof(float));
      // Add the copied data size in bytes to the offset
      offset += sizeof(float);
  
      std::memcpy(&amp;b, data + offset, sizeof(vector));
      offset += sizeof(vector);
  
      std::memcpy(&amp;c, data + offset, sizeof(bool));
      offset += sizeof(bool);
  
      // It all works until here the results are the same as the ones I assigned
      // however the int value becomes 83886080 instead of 5
      // moving this above the bool memcpy (and moving the variable in the struct too) fixes the problem
      std::memcpy(&amp;d, data + offset, sizeof(int));
      offset += sizeof(int);
  
      return 0;
  }
  </code></pre>
  
  <p>So I expected the value of d to be 5 however it becomes 83886080 which I presume is just random uninitialized memory.</p>