title: Loss of data while building a std::string from const char * or LPCSTR
body: <p>I have a Function which returns a LPSTR/const char * and I need to convert it to a std::string. This is how I am doing it.</p>
  
  <pre><code>std::string szStr(foo(1));
  </code></pre>
  
  <p>It works just fine in all the cases just when foo returns a 32 characters long string it fails. With this approach I get <code>""</code>. So I thought it had to do something with the length. So I changed it a bit.</p>
  
  <pre><code>std::string szStr(foo(1) , 32);
  </code></pre>
  
  <p>This gives me <code>"0"</code></p>
  
  <p>Then I tried another tedious method</p>
  
  <pre><code>const char * cstr_a = foo(1);
  const char * cstr_b = foo(2);
  
  size_t ln_a = strlen(cstr_a);
  size_t ln_b = strlen(cstr_b);
  
  std::string szStr_a( cstr_a , ln_a );
  std::string szStr_b( cstr_b , ln_b );
  </code></pre>
  
  <p>But strangely enough in this method both the pointers are getting the same value, viz foo(1) should return abc and foo(2) should return xyz. But here cstr_a is first getting abc but the moment cstr_b gets xyz, the value of both cstr_a and cstr_b becomes xyz. I am dazed and confused with this.</p>
  
  <p>And yes, I cannot use std::wstring.</p>
  
  <p><strong>What is foo?</strong></p>
  
  <p>foo is basically reading a value from the registry and returning it as a LPSTR. Now one the value in the registry which I need to read is a MD5 hashed string (32 charecters) That's where it fails.</p>
  
  <p><strong>The Actual Foo function:</strong></p>
  
  <pre><code>LPCSTR CRegistryOperation::GetRegValue(HKEY hHeadKey, LPCSTR szPath, LPCSTR szValue)
  {
      HKEY hKey;
      CHAR szBuff[255] = ("");
      DWORD dwBufSize = 255;
  
      ::RegOpenKeyEx(hHeadKey, (LPCSTR)szPath, 0, KEY_READ, &amp;hKey);
      ::RegQueryValueEx(hKey, (LPCSTR)szValue, NULL, 0, (LPBYTE)szBuff, &amp;dwBufSize);
      ::RegCloseKey(hKey);
  
      LPCSTR cstr(szBuff);
  
      return cstr;
  }
  </code></pre>
  
  <p><strong>The Original cast code:</strong></p>
  
  <pre><code>StrResultMap RegValues;
  std::string lid(CRegistryOperation::GetRegValue(HKEY_CURRENT_USER, REG_KEY_HKCU_PATH, "LicenseID"));
  std::string mid(CRegistryOperation::GetRegValue(HKEY_CURRENT_USER, REG_KEY_HKCU_PATH, "MachineID"), 32);
  std::string vtill(CRegistryOperation::GetRegValue(HKEY_CURRENT_USER, REG_KEY_HKCU_PATH, "ValidTill"));
  std::string adate(CRegistryOperation::GetRegValue(HKEY_CURRENT_USER, REG_KEY_HKCU_PATH, "ActivateDT"));
  std::string lupdate(CRegistryOperation::GetRegValue(HKEY_CURRENT_USER, REG_KEY_HKCU_PATH, "LastUpdate"));
  
  RegValues["license_id"] = lid;
  RegValues["machine_id"] = mid;
  RegValues["valid_till"] = vtill;
  RegValues["activation_date"] = adate;
  RegValues["last_updated"] = lupdate;
  </code></pre>
  
  <p>Kindly help me get over it.
  Thanks.</p>