What is the easiest way to make a C++ program cras

2019-01-10 00:13发布

I'm trying to make a Python program that interfaces with a different crashy process (that's out of my hands). Unfortunately the program I'm interfacing with doesn't even crash reliably! So I want to make a quick C++ program that crashes on purpose but I don't actually know the best and shortest way to do that, does anyone know what to put between my:

int main() {
    crashyCodeGoesHere();
}

to make my C++ program crash reliably

标签: c++ crash
28条回答
爷、活的狠高调
2楼-- · 2019-01-10 00:52

A stylish way of doing this is a pure virtual function call:

class Base;

void func(Base*);

class Base
{
public:
   virtual void f() = 0;
   Base() 
   {
       func(this);
   }
};

class Derived : Base
{
   virtual void f()
   {
   }
};

void func(Base* p)
{
   p->f();
}


int main()
{
    Derived  d;
}

Compiled with gcc, this prints:

pure virtual method called

terminate called without an active exception

Aborted (core dumped)

查看更多
够拽才男人
3楼-- · 2019-01-10 00:53

Dividing by zero will crash the application:

int main()
{
    return 1 / 0;
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-10 00:54

The abort() function is probably your best bet. It's part of the C standard library, and is defined as "causing abnormal program termination" (e.g, a fatal error or crash).

查看更多
做个烂人
5楼-- · 2019-01-10 00:55
 throw 42;

Just the answer... :)

查看更多
登录 后发表回答