Bus error vs Segmentation fault

前端 未结 7 773
梦毁少年i
梦毁少年i 2020-12-24 01:19

Difference between a bus error and a segmentation fault? Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bu

7条回答
  •  执笔经年
    2020-12-24 02:16

    This would be a dup of What is a bus error?, if it weren't for the

    Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ?

    part of the question. You should be able to answer this for yourself with the information found here.


    Insanity: doing the same thing over and over again and expecting different results.
    -- Albert Einstein


    Of course, taking the question literally...

    #include 
    #include 
    #include 
    #include 
    int main() {
        srand(time(NULL));
        if (rand() % 2)
            kill(getpid(), SIGBUS);
        else
            kill(getpid(), SIGSEGV);
        return 0;
    }
    

    Tada, a program that can exit with a segmentation fault on one run and exit with a bus error on another run.

提交回复
热议问题