c++ - 返回 std::string 的函数在没有 return 语句的情况下崩溃,这与返回 int 且没有 return 语句的函数不同

标签 c++ crash return return-value return-type

#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() 执行时没有问题。为什么没有 return 语句返回字符串的函数会崩溃?

最佳答案

两者都是未定义的行为,即使是noCrash也会崩溃。

关于c++ - 返回 std::string 的函数在没有 return 语句的情况下崩溃,这与返回 int 且没有 return 语句的函数不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11096027/

相关文章:

objective-c - 应用程序因警报 View 而崩溃

java - 方法结束时对java返回的混淆

java - 返回我的图书馆系统时出现问题

c++ - 从文件中读取优先级队列 C++

C++:条件变量等待

c++ - 调用头文件中声明的函数

c++ - 查找较新版本的应用程序

c++ - 将 C++/Win32 控制台应用程序转换为 DLL 后崩溃

crash - 添加具有导航属性的实体对象作为数据源,导致 Visual Studio 2015 崩溃

JavaScript:调用 init 函数与返回该函数调用