title: Deep copy of TCHAR array is truncated
body: <p>I've created a class to test some functionality I need to use.  Essentially the class will take a deep copy of the passed in string and make it available via a getter. Am using Visual Studio 2012.  Unicode is enabled in the project settings.</p>
  
  <p>The problem is that the memcpy operation is yielding a truncated string. Output is like so;</p>
  
  <pre><code>THISISATEST: InstanceDataConstructor: Testing testing 123
  Testing te_READY
  </code></pre>
  
  <p>where the first line is the check of the passed in TCHAR* string &amp; the second line is the output from populating the allocated memory with the memcpy operation.  Output expected is; "Testing testing 123".</p>
  
  <p>Can anyone explain what is wrong here?</p>
  
  <p>N.B. Got the #ifndef UNICODE typedefs from here: <a href="https://stackoverflow.com/questions/6291458/how-to-convert-tchar-array-to-stdstring/6291495#6291495">how-to-convert-tchar-array-to-stdstring</a></p>
  
  <pre><code>#ifndef INSTANCE_DATA_H//if not defined already
  #define INSTANCE_DATA_H//then define it
  
  #include &lt;string&gt;
  
  //TCHAR is just a typedef, that depending on your compilation configuration, either defaults to char or wchar.
  //Standard Template Library supports both ASCII (with std::string) and wide character sets (with std::wstring).
  //All you need to do is to typedef String as either std::string or std::wstring depending on your compilation configuration.
  //To maintain flexibility you can use the following code:
  #ifndef UNICODE  
    typedef std::string String; 
  #else
    typedef std::wstring String; 
  #endif
  //Now you may use String in your code and let the compiler handle the nasty parts. String will now have constructors that lets you convert TCHAR to std::string or std::wstring.
  
  
  class InstanceData
  {
  public: 
      InstanceData(TCHAR* strIn) : strMessage(strIn)//constructor     
          {
          //Check to passed in string
          String outMsg(L"THISISATEST: InstanceDataConstructor: ");//L for wide character string literal
          outMsg += strMessage;//concatenate message
          const wchar_t* finalMsg = outMsg.c_str();//prepare for outputting
          OutputDebugStringW(finalMsg);//print the message    
  
          //Prepare TCHAR dynamic array.  Deep copy.
          charArrayPtr = new TCHAR[strMessage.size() +1];
          charArrayPtr[strMessage.size()] = 0;//null terminate
          std::memcpy(charArrayPtr, strMessage.data(), strMessage.size());//copy characters from array pointed to by the passed in TCHAR*.
  
          OutputDebugStringW(charArrayPtr);//print the copied message to check    
          }
  
      ~InstanceData()//destructor
          {
              delete[] charArrayPtr;
          }
  
  //Getter
  TCHAR* getMessage() const
  {
      return charArrayPtr;
  }
  
  private:
      TCHAR* charArrayPtr;
      String strMessage;//is used to conveniently ascertain the length of the passed in underlying TCHAR array.
  };
  #endif//header guard
  </code></pre>