请教 C++大佬,const string&和 char*返回值的转换问题.

2019-08-21 10:58:05 +08:00
 xmyjd

下面代码 f()输出的字符串不是"qqq""www"之类.输出不正常.这是什么原理? gcc 4.8.5

#include<string>
#include<cstdio>
#include<iostream>
using namespace std;

const string &f(){
    static int n=0;
    static const char* const c[]={"qqq","www","eee","rrr","ttt","yyy","uuu","iii"};
    return c[n++];
}

int main(int argc, char** argv)
{
    cout << f() << endl;
    cout << f() << endl;
    cout << f()<< "111" << f() <<endl;
    printf("%s%s%s\n",f().c_str(),"222",f().c_str());
    printf("%s\n",f().c_str());
    printf("%s\n",f().c_str());
    return 0;
}
5368 次点击
所在节点    C++
14 条回复
andyzhshg
2019-08-21 11:07:03 +08:00
你这代码相当于这样:

```c++
const string &f(){
static int n=0;
static const char* const c[]={"qqq","www","eee","rrr","ttt","yyy","uuu","iii"};
string res = c[n++];
return res;
}
```

res 是局部变量,返回局部变量的引用是不行的
wutiantong
2019-08-21 11:08:43 +08:00
Don't return reference to temporary object
GPIO
2019-08-21 11:11:10 +08:00
string f(){
static int n=0;
static const char* const c[]={"qqq","www","eee","rrr","ttt","yyy","uuu","iii"};
return c[n++];
}
mattxlee
2019-08-21 11:11:25 +08:00
return c[n++]使用隐式转换构建了一个临时的 std::string 然后返回了它的引用,这样是不对的。你直接把函数 f 的返回值改成 string (去掉 const 和&)就好了
string f() {
...
}
AlohaV2
2019-08-21 11:12:03 +08:00
你留意一下编译器有没有报 Wreturn-local-addr 错误。
f() return c[n++]相当于 在 f()函数的栈上构造了 string()类型,把 c[index]的字符串复制了一份。return 时然后把它的引用作为返回值。
不巧的是 f()调用完之后,也就是 cout<< 想去拿输入的时候,f()调用完了,栈上的内容已经析构掉了( string 没了)。
把返回值的&符号去掉可以解决这个问题。
GeruzoniAnsasu
2019-08-21 12:01:46 +08:00
Never return reference to temporary object


在实现了移动语义和 RVO 的 c++11 后标准下
string a = get_str() 完全可以被优化成
char *a = str (包含临时对象在内总共只有一次指针赋值)
对于 stl 容器来说,甚至完全不需要使用引用,一律传 /返回对象 一把梭就行(此论断于理解&&后失效)
inhzus
2019-08-21 12:33:51 +08:00
歪个楼,“说点奇淫技巧”
既然 C++ 的这些字符串在程序运行是都分布在 read-only memory,那么尽管他是局部变量,但你完全是可以读到的。比如
wutiantong
2019-08-21 15:23:27 +08:00
@inhzus string literal 不是局部变量,谢谢
elfive
2019-08-21 16:03:31 +08:00
C++11 标准下,编译器会在返回局部变量的常量引用是临时扩大改临时变量的生命周期。
( The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.)
来源:
https://en.cppreference.com/w/cpp/language/lifetime

直接返回 const char*,感觉更好。
wutiantong
2019-08-21 16:12:28 +08:00
@elfive 你说的这个,是在 C++11 之前就有的,C++11 进一步补充了 rvalue reference 的 case。

具体到这个帖子里的代码,则需要进一步参考“ reference initialization ”链接的内容,摘录如下:

a temporary bound to a return value of a function in a return statement is not extended: it is destroyed immediately at the end of the return expression. Such function always returns a dangling reference.

来源:
https://en.cppreference.com/w/cpp/language/reference_initialization#Lifetime_of_a_temporary
lcdtyph
2019-08-21 16:18:28 +08:00
@elfive
你说的这个不适用于直接返回 const std::string & 的情况,
const std::string &foo();
const std::string &bar = foo(); /* 悬空引用 */

但是可以延长直接返回 std::string 临时对象的生命周期。
std::string foo();
const std::string &bar = foo();
上述代码是没问题的
Cyshall
2019-08-21 17:35:20 +08:00
你函数返回值类型是 string,而你的返回值是 char 类型,返回的时候会触发隐式转换从而产生临时变量,此时就是返回临时变量的引用自然会出现问题。
wisan
2019-08-21 19:16:06 +08:00
你要返回引用,但从 char*转为 string 是个右值。。。
cyyzero
2019-08-22 05:58:21 +08:00
dangling reference

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/593758

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX