Even if you make abc global, it still doesn't work in Visual Studio 2015
Because your function is returning a copy of abc and that copy is a temporary object that goes out of scope at the sequence point, just as deceptikon explained.
returnValue seems to point to I'm not sure where, but clearly it's not pointing to whatever c_str() returned?
returnValue
is exactly equal to the pointer retunred by c_str()
- after all you just assigned it to that on the line before. However you called c_str()
on the temporary object, not on abc
and c_str()
on the temporary object returns a different pointer than c_str()
on abc
, specifically it returns a pointer to temporary storage that gets freed when the temporary goes out of scope, which is why you see those results.