本文翻译自:What is the easiest way to make a C++ program crash?
I'm trying to make a Python program that interfaces with a different crashy process (that's out of my hands). 我正在尝试制作一个与不同的崩溃过程接口的Python程序(这不在我的手中)。 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: 所以我想制作一个快速崩溃的C ++程序,但我实际上并不知道最好和最短的方法,有谁知道我的:
int main() {
crashyCodeGoesHere();
}
to make my C++ program crash reliably 使我的C ++程序可靠地崩溃
#1楼
参考:https://stackoom.com/question/ZaUx/使C-程序崩溃的最简单方法是什么
#2楼
Or another way since we're on the band wagon. 或者另一种方式,因为我们在乐队旅行车上。
A lovely piece of infinite recursion. 一段可爱的无限递归。 Guaranteed to blow your stack. 保证吹你的筹码。
int main(int argv, char* argc)
{
return main(argv, argc)
}
Prints out: 打印出来:
Segmentation fault (core dumped) 分段故障(核心转储)
#3楼
This crashes on my Linux system, because string literals are stored in read only memory: 这在我的Linux系统上崩溃,因为字符串文字存储在只读内存中:
0[""]--;
By the way, g++ refuses to compile this. 顺便说一下,g ++拒绝编译它。 Compilers are getting smarter and smarter :) 编译器变得更聪明,更聪明:)
#4楼
void main()
{
int *aNumber = (int*) malloc(sizeof(int));
int j = 10;
for(int i = 2; i <= j; ++i)
{
aNumber = (int*) realloc(aNumber, sizeof(int) * i);
j += 10;
}
}
Hope this crashes. 希望这会崩溃。 Cheers. 干杯。
#5楼
int main(int argc, char *argv[])
{
char *buf=NULL;buf[0]=0;
return 0;
}
#6楼
int main()
{
int *p=3;
int s;
while(1) {
s=*p;
p++;
}
}