简体   繁体   English

CPP 控制台应用程序退出而不提供错误信息

[英]CPP console applications exits without providing error information

When I compile and run the below piece of code the exe crashes yet doesn't provide information of any sort regarding why it crashed.当我编译并运行以下代码时,exe 崩溃但没有提供任何关于它为什么崩溃的信息。 (Seg faults etc. not reported). (未报告段故障等)。 Here is the sample code that I tried:这是我尝试过的示例代码:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector <int> values;
    int temp = values.back();
    cout << temp << endl;
    return 0;
}

The same piece of code when compiled and run-on Linux produce a seg fault.同一段代码在 Linux 上编译和运行时会产生段错误。 Is there something to be configured specifically in windows to let console applications generate information regarding runtime errors?是否需要在 windows 中专门配置某些内容,以让控制台应用程序生成有关运行时错误的信息?

Calling std::vector::back on an empty vector is undefined behavior .在空向量上调用std::vector::back未定义的行为 The compiler or runtime aren't required to emit a diagnostic.编译器或运行时不需要发出诊断。 There might be a tool or sanitizer that could help depending on the compiler you're using.根据您使用的编译器,可能有一个工具或消毒剂可以提供帮助。

Is there something to be configured specifically in windows to let console applications generate information regarding runtime errors?是否需要在 windows 中专门配置某些内容,以让控制台应用程序生成有关运行时错误的信息?

The problem is that value.back() causes undefined behavior (because your vector is empty).问题是value.back()导致未定义的行为(因为你的向量是空的)。 The effect of undefined behavior is undefined.未定义行为的影响是未定义的。 It might result in a seg fault, but also could just exit the application.它可能会导致段错误,但也可能会退出应用程序。 You can't enforce a segfault for that, and not really enforce a crash.您不能为此强制执行段错误,也不能真正强制崩溃。

Using static analyzers and increasing the warning level can help in certain cases.在某些情况下,使用 static 分析仪并提高警告级别会有所帮助。 But there is no way to detect all possible errors.但是没有办法检测所有可能的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM