Safe way to cast a uint16_t to a wchar_t
<p>Trying to clean up some code and i wanted to know if the following is a safe way to cast uint16_t to a wchar_t.</p>

<pre><code>#if ! defined(MARKUP_SIZEOFWCHAR)
#if __SIZEOF_WCHAR_T__ == 4 || __WCHAR_MAX__ &gt; 0x10000
#define MARKUP_SIZEOFWCHAR 4
#else
#define MARKUP_SIZEOFWCHAR 2
#endif

void FileReader::parseBuffer(char * buffer, int length)
{
  //start by looking for a vrsn
  //Header seek around for a vrns followed by 32 bit size descriptor
  //read 32 bits at a time
  int cursor = 0;
  char vrsn[5] = "vrsn";
  cursor = this-&gt;searchForMarker(cursor, length, vrsn, buffer);
  int32_t size = this-&gt;getObjectSizeForMarker(cursor, length, buffer);
  cursor = cursor + 7; //advance cursor past marker and size
  wchar_t *version = this-&gt;getObjectForSizeAndCursor(size, cursor, buffer);
  wcout &lt;&lt; version;
  delete[] version; //this pointer is dest from getObjectForSizeAndCursor
}
</code></pre>

<p>-</p>

<pre><code>wchar_t* FileReader::getObjectForSizeAndCursor(int32_t size, int cursor, char *buffer) {

  int wlen = size/2;
  uint32_t *dest = new uint32_t[wlen+1];
  unsigned char *ptr = (unsigned char *)(buffer + cursor);
  for(int i=0; i&lt;wlen; i++) {
    #if MARKUP_SIZEOFWCHAR == 4 // sizeof(wchar_t) == 4
      char padding[2] = {'\0','\0'}; 
      dest[i] =  (padding[0] &lt;&lt; 24) + (padding[1] &lt;&lt; 16) + (ptr[0] &lt;&lt; 8) + ptr[1];
    #else // sizeof(wchar_t) == 2
      dest[i] = (ptr[0] &lt;&lt; 8) + ptr[1];
    #endif
      ptr += 2;
      cout &lt;&lt; ptr;
  }
  return (wchar_t *)dest;
}
</code></pre>

<p>do i have any scoping issues with the way i am using the padding? will i leak padding when i <code>delete dest[]</code> in the calling function?</p>
