Minimal C/C++ program that segfaults? [duplicate]

安稳与你 提交于 2019-12-23 04:36:09

问题


I'm trying to set up the way my server handles core dumps. In order to test it, I'd need a program that always segfaults.

Is there a simple example program that always segfaults?


回答1:


main;

is portable, and segfault in 5chars.




回答2:


main() { *(int *)0xdeadbeef = 37; } should do it.




回答3:


try this:

long* ptr = 0x0; //-- you can also use other random values and likely you will segfault
printf("%f", *ptr);



回答4:


You can try:

main() {
char *p = NULL;
char c = *p;
}



回答5:


this should die:

int main() {
    char *die;
    printf("%d",(int *)die * 200);
    return 0;
}

edit:

int main() {
    char *die;
    int killer = 200;
    while(1) {
       printf("%d",(int *)die * killer);
       killer = killer * killer;
    }
    return 0;
}


来源:https://stackoverflow.com/questions/12404759/minimal-c-c-program-that-segfaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!