简体   繁体   English

如何中止c ++程序并以状态0退出?

[英]how to abort a c++ program and exit with status 0?

I want to kill my c++ program and terminate immediately without activating destructors of any kind, especially static and global variables, but I want to exit with status 0 - abort() will not work for me. 我想杀死我的c ++程序并立即终止而不激活任何类型的析构函数,尤其是静态和全局变量,但我想退出状态0 - abort()对我不起作用。

Does anyone have a solution? 有没有人有办法解决吗? Thanks 谢谢

Maybe _exit(0); 也许_exit(0); is what you're looking for? 你正在寻找什么?

Here is the man page to read up about it. 这是阅读它的手册页

From C++11 n3290 - § 18.5: 从C ++ 11 n3290 - §18.5:

[[noreturn]] void _Exit(int status) noexcept;

The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() 程序终止而不执行自动,线程或静态存储持续时间对象的析构函数,并且不调用传递给atexit()的函数

This is actually defined in C99 though so in practice works on a large number of pre-C++11 implementations. 这实际上是在C99中定义的,但实际上它适用于大量的C ++ 11之前的实现。

Use: 采用:

#include <cstdlib>
#include <iostream>

struct test {
  ~test() {
    std::cout << "Goodbye world" << std::endl;
  }
};

int main() {
  test t;
  _Exit(0);
}

How about _Exit(0) from stdlib.h . 来自stdlib.h _Exit(0)怎么样? (Demo: http://ideone.com/ecCgC ) (演示: http//ideone.com/ecCgC

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

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