xlC_r对c_str的处理

在xlC_r下编译如下代码:

#include <iostream>

static std::string s_strHello = "Hello";

std::string getString()
{
    return s_strHello;
}

int main(void)
{
    const char *szHello = getString().c_str();
    printf("%s\n", szHello);
    return 0;
}

 

编译运行结果如下:

[tortoise@sea temp]$ xlC_r -o test test.cpp
[tortoise@sea temp]$ ./test

[tortoise@sea temp]$ 

但是同样的代码在g++下却没有问题:

[tortoise@sea temp]$ g++ -o test test.cpp
[tortoise@sea temp]$ ./test
Hello
[tortoise@sea temp]$ 

 

getString()返回的是一个临时对象,那c_str()返回的指向内容的指针,在这个临时对象销毁之后是否还应该存在呢?

posted @ 2014-10-27 10:45  StupidTortoise  阅读(284)  评论(0编辑  收藏  举报