请教 C++大佬,const string&和 char*返回值的转换问题. - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xmyjd
V2EX    C++

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

  •  
  •   xmyjd 2019-08-21 10:58:05 +08:00 6191 次点击
    这是一个创建于 2306 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    #include<string> #include<ctdio> #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; } 
    14 条回复    2019-08-22 05:58:21 +08:00
    andyzhshg
        1
    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
        2
    wutiantong  
       2019-08-21 11:08:43 +08:00
    Don't return reference to temporary object
    GPIO
        3
    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
        4
    mattxlee  
       2019-08-21 11:11:25 +08:00
    return c[n++]使用隐式转换构建了一个临时的 std::string 然后返回了它的引用,这样是不对的。你直接把函数 f 的返回值改成 string (去掉 const 和&)就好了
    string f() {
    ...
    }
    AlohaV2
        5
    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
        6
    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
        7
    inhzus  
       2019-08-21 12:33:51 +08:00
    歪个楼,“说点奇淫技巧”
    既然 C++ 的这些字符串在程序运行是都分布在 read-only memory,那么尽管他是局部变量,但你完全是可以读到的。比如
    wutiantong
        8
    wutiantong  
       2019-08-21 15:23:27 +08:00
    @inhzus string literal 不是局部变量,谢谢
    elfive
        9
    elfive  
       2019-08-21 16:03:31 +08:00 via iPhone
    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
        10
    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
        11
    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
        12
    Cyshall  
       2019-08-21 17:35:20 +08:00
    你函数返回值类型是 string,而你的返回值是 char 类型,返回的时候会触发隐式转换从而产生临时变量,此时就是返回临时变量的引用自然会出现问题。
    wisan
        13
    wisan  
       2019-08-21 19:16:06 +08:00 via Android
    你要返回引用,但从 char*转为 string 是个右值。。。
    cyyzero
        14
    cyyzero  
       2019-08-22 05:58:21 +08:00 via Android
    dangling reference
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2744 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 09:16 PVG 17:16 LAX 01:16 JFK 04:16
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86