title: C++. reinterpret_cast from double to unsigned char*
body: <p>I was having a small play around with C++ today and came across this which I thought was odd, but perhaps more likely due to a misunderstanding by me and lack of pure C coding recently.</p>
  
  <p>What I was looking to do originally was convert a double into an array of unsigned chars. My understanding was that the 64 bits of the double (sizeof(double) is 8) would now be represented as 8 8-bit chars. To do this I was using reinterpret_cast.</p>
  
  <p>So here's some code to convert from double to char array, or at least I thought that's what it was doing. Problem was it was returning 15 from strlen instead of 8, why I'm not sure.</p>
  
  <pre><code>double d = 0.3;
  
  unsigned char *c = reinterpret_cast&lt;unsigned char*&gt; ( &amp;d );
  
  std::cout &lt;&lt; strlen( (char*)c ) &lt;&lt; std::endl;
  </code></pre>
  
  <p>So the strlen was my first issue. But then I tried the following and found that it returned 11, 19, 27, 35. The difference between these numbers is 8 so on some level something right is going on. But why does this not return 15, 15, 15, 15, ( as it was returning 15 in the code above ).</p>
  
  <pre><code>double d = 0.3;
  double d1 = 0.3;
  double d2 = 0.3;
  double d3 = 0.3;
  
  unsigned char *c_d = reinterpret_cast&lt;unsigned char*&gt; ( &amp;d );
  unsigned char *c_d1 = reinterpret_cast&lt;unsigned char*&gt; ( &amp;d1 );
  unsigned char *c_d2 = reinterpret_cast&lt;unsigned char*&gt; ( &amp;d2 );
  unsigned char *c_d3 = reinterpret_cast&lt;unsigned char*&gt; ( &amp;d3 );
  
  std::cout &lt;&lt; strlen( (char*)c_d ) &lt;&lt; std::endl;
  std::cout &lt;&lt; strlen( (char*)c_d1 ) &lt;&lt; std::endl;
  std::cout &lt;&lt; strlen( (char*)c_d2 ) &lt;&lt; std::endl;
  std::cout &lt;&lt; strlen( (char*)c_d3 ) &lt;&lt; std::endl;
  </code></pre>
  
  <p>So I looked at the addresses of the chars and they are.</p>
  
  <pre><code>0x28fec4
  0x28fec0
  0x28febc
  0x28feb8 
  </code></pre>
  
  <p>Now this makes sense given that the size of an unsigned char* on my system is 4 bytes, but I thought the correct amount of memory would be allocated from the cast, otherwise it seems like reinterpret_cast is a pretty dangerous thing... Furthermore if I do</p>
  
  <pre><code>for (int i = 0; i &lt; 4; ++i) {
      double d = 0.3;
  
      unsigned char *c = reinterpret_cast&lt;unsigned char*&gt; ( &amp;d );
  
      std::cout &lt;&lt; strlen( (char*)c ) &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>This prints 11, 11, 11, 11!</p>
  
  <p>So what is going on here, clearly memory is getting overwritten in places and reinterpret cast is not working as I thought it would (i.e. I'm using it wrong). Having been using strings for so long in C++, sometimes when you go back to raw char arrays, you forget these things.</p>
  
  <p>So I suppose this is a 3 part question.</p>
  
  <p>Why was strlen initially returning 15?
  Why did the 4 strlen calls grow in size?
  Why did the loop return 11, 11, 11, 11?</p>
  
  <p>Thanks.</p>