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
This is a more guaranteed version of abort presented in above answers.It takes care of the situation when sigabrt is blocked.You can infact use any signal instead of abort that has the default action of crashing the program.
C++ is can be crashed deterministically by having 2 exceptions in parallel! The standard says never throw any exception from a destructor OR never use any function in a destructor which may throw exception.
we have to make a function so lets leave the destructor etc etc.
An example from ISO/IEC 14882 §15.1-7. Should be a crash as per C++ standard. Ideone example can be found here.
ISO/IEC 14882 §15.1/9 mentions throw without try block resulting in implicit call to abort:
Others include : throw from destructor: ISO/IEC 14882 §15.2/3
One that has not been mentioned yet:
This will treat the null pointer as a function pointer and then call it. Just like most methods, this is not guaranteed to crash the program, but the chances of the OS allowing this to go unchecked and of the program ever returning are negligible.
The answer is platform specific and depends on your goals. But here's the Mozilla Javascript crash function, which I think illustrates a lot of the challenges to making this work:
Although this question already has an accepted answer...
Or...
void main(){throw 1;}
Hope this crashes. Cheers.