没有返回语句的情况下返回std :: string的函数崩溃,这与没有返回语句的情况下返回int的函数不同 [英] function returning std::string crashes without return statement, unlike a function returning int without return statement

查看:83
本文介绍了没有返回语句的情况下返回std :: string的函数崩溃,这与没有返回语句的情况下返回int的函数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

string
crash()
{

}

int
noCrash()
{

}

int
main()
{
    crash(); // crashes
    // noCrash(); // doesn't crash
    return 0;
}

函数crash()在Mingw g ++ 4.6.2中崩溃,而函数noCrash()在执行时没有任何问题.为什么没有返回语句的函数返回字符串会崩溃?

The function crash(), crashes with Mingw g++ 4.6.2 and the function noCrash() executes with no issues. Why does the function returning string crash without a return statement?

推荐答案

这都是未定义的行为,即使 noCrash 也会崩溃.

Both are undefined behaviors, even noCrash can crash.

这篇关于没有返回语句的情况下返回std :: string的函数崩溃,这与没有返回语句的情况下返回int的函数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆