Is it safe to take the address of std::wstring's internal pointer?
Posted
by LCC
on Stack Overflow
See other posts from Stack Overflow
or by LCC
Published on 2010-06-12T20:18:31Z
Indexed on
2010/06/12
20:22 UTC
Read the original article
Hit count: 201
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.
© Stack Overflow or respective owner