title: Using `memcpy()` to assign a pointer an address
body: <p>Is</p>
  
  <pre><code>memcpy( ptr, &amp;value, sizeof(char *) );
  </code></pre>
  
  <p>the same as</p>
  
  <pre><code>ptr = &amp;value;
  </code></pre>
  
  <p>.....</p>
  
  <p>I've never seen <code>memcpy()</code> used to do this before.
  It seems like I should respond to the developer of this code with the phrase "Well, you're not wrong." It seems like a bit wordier than it needs to be.</p>
  
  <p>Edit: It seems important to note that <code>ptr</code> is <code>void * ptr</code>. <code>value</code> is <code>double value</code>.</p>
  
  <hr>
  
  <p>Edit2: For those who are interested in following this problem down its rabbit hole with me read along.  This bit of code was flagged by static analysis as a potential buffer overflow.  The static analysis was worried that the destination buffer <code>ptr</code> might not have enough space to hold a <code>sizeof(char *)</code> worth of information.<br>
  Rather than deal with the problem it gave me I tried to understand exactly what this <code>memcpy()</code> did (thank you for helping me with that).  Once I understood what was happening, I had to understand why the size of memory given to <code>memcpy()</code> was <code>sizeof(char *)</code>; the function this <code>memcpy()</code> is in is called one time, and that one time it looks like this:</p>
  
  <pre><code>SdmJniGetPointer(blah, blah, &amp;ptr)
  </code></pre>
  
  <p>Meaning that the actual value of <code>ptr</code> within this function, and in the <code>memcpy()</code> call, is the address of another pointer!</p>
  
  <p>It was bizzare to me that the previous developer would <code>memcpy()</code> a <code>sizeof(char *)</code> amount of data because this system is compiled on 32 and 64 bit machines.  However, because he passes in the address to a pointer (<code>&amp;ptr</code>) into the function as <code>ptr</code>, he not only circumvents this shifting from 4-byte pointers to 8-byte pointers depending on the system architecture but wields it exactly as he meant too (I think).  In the end, on a 64-bit system, all of the <code>double value</code> is copied into the address pointed to by <code>ptr</code> which is really an address to a different pointer (<code>&amp;ptr</code>), and on 32-bit machines, half of it is copied to the location <code>ptr</code> which is really <code>&amp;ptr</code>.  Now I just need to figure out what that <code>double value</code> really represents, and why he would be ok bisecting it.</p>
  
  <p>If that didn't make sense, I'll try again, but it feels a like he was Inception-ing the pointer. =)</p>