title: Converting managed Byte array to unmanaged string
body: <p>I am trying to convert a managed byte array to std::string in my C++/CLI wrapper; however, I am seeing some corrupt memory in the heap later on.  Just wanted to check if I am doing the conversion right.  Below is my method is CLI:</p>
  
  <pre><code>string ByteArrayToStr(array&lt;Byte&gt;^ byteArray)
  {
      int size = byteArray.Length;
      IntPtr pnt = Marshal::AllocHGlobal(size + 1);
      char* chararray = (char*)pnt.ToPointer();
      try
      {
          Marshal::Copy(byteArray, 0, pnt, size);
          *(chararray + size) = 0;
          return string(chararray);
      }
      finally
      {
          Marshal::FreeHGlobal(pnt);
      }
  }
  </code></pre>
  
  <p>Does anything seem wrong in above code?</p>