V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xmyjd
V2EX  ›  C++

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

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

    下面代码 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;
    }
    
    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   ·   我们的愿景   ·   实用小工具   ·   4436 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:10 · PVG 18:10 · LAX 03:10 · JFK 06:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.