The following code with Visual Studio 2013 produces unexpected results when getting a const char * directly from a std::ostringstream but correct results when using a intermediate std::string. Why is that?
#include <string>
#include <sstream>
#include <string>
#include <iostream>
int main()
{
    std::ostringstream oss;
    oss << "Hello work" << std::endl;
    const char *charPtr = oss.str().c_str(); // Unexpected results
    std::string str = oss.str();
    const char *charPtr2 = str.c_str(); // OK
    std::cout << "charPtr is: " << charPtr << std::endl;
    std::cout << "charPtr2 is: " << charPtr2 << std::endl;
    char c;
    std::cin >> c;
    return 0;
}
Produces
charPtr is:
charPtr2 is: Hello work
 
     
     
     
    