Is it safe to take the address of std::wstring's internal pointer?
- by LCC
I have an interface which is used like the following:
if (SUCCEEDED(pInterface->GetSize(&size))
{
wchar_t tmp = new wchar_t[size];
if (SUCCEEDED(pInterface->GetValue(tmp, size)))
{
std::wstring str = tmp;
// do some work which doesn't throw
}
delete[] tmp;
}
Is it safe and portable to do this instead?
if (SUCCEEDED(pInterface->GetSize(&size))
{
std::wstring str;
str.resize(size);
if (SUCCEEDED(pInterface->GetValue(&str[0], size)))
{
// do some work
}
}
Now, obviously this works (doesn't crash/corrupt memory) or I wouldn't have asked, but I'm mostly wanting to know if there's a compelling reason not to do this.