title: Is this defined behavior? Store data as char array
body: <p>I was playing with an idea for dynamic size memory pool, and playing, I end up with some code that I was certain that will fail, but didn't. Here's the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  struct largeStruct
  {
      largeStruct()
      {
          X = 123456789;
          Y = 987654321;
          str = "Hy! Here I am, a c string, out in the wild c++, the place where your pointers dangle and your friends play with your privates";
      }
      unsigned long long X;
      unsigned long long Y;
      char* str;
  };
  
  int main()
  {
      void* ptr = new unsigned char[sizeof(largeStruct)];
  
      largeStruct a;
      *((largeStruct*)ptr) = a;
  
      cout &lt;&lt; "Size of data: " &lt;&lt; sizeof(largeStruct) &lt;&lt; endl;
      cout &lt;&lt; "Data: " &lt;&lt; endl;
      cout &lt;&lt; ((largeStruct*)ptr)-&gt;X &lt;&lt; endl
           &lt;&lt; ((largeStruct*)ptr)-&gt;Y &lt;&lt; endl
           &lt;&lt; ((largeStruct*)ptr)-&gt;str &lt;&lt; endl
           &lt;&lt; endl;
  
      delete[] ptr;
  }
  </code></pre>
  
  <p>That works in my pc ( Windows 8, MSVC Express 2012). Putting some more though into it, i think that, while a hack, it makes some sense.
  A array in memory is like this:</p>
  
  <pre><code>===================================
  ... ||  a0  ||  a1  ||  a2   || ...
  ===================================
  </code></pre>
  
  <p>So when you try to store a values that is bigger than one of your array members, it access the memory on the side. So try to store a value of 3 chunks size will work, and end up as this:</p>
  
  <pre><code>====================================
  ... || data  data  data  data || ...
  ====================================
  </code></pre>
  
  <p>So, is this dangerous, or is some kind of obscure harmless hack?</p>