What is the simplest standard conform way to produce a Segfault in C?

后端 未结 10 1646
迷失自我
迷失自我 2020-11-29 21:59

I think the question says it all. An example covering most standards from C89 to C11 would be helpful. I though of this one, but I guess it is just undefined behaviour:

10条回答
  •  囚心锁ツ
    2020-11-29 22:32

    If we assume we are not raising a signal calling raise, segmentation fault is likely to come from undefined behavior. Undefined behavior is undefined and a compiler is free to refuse to translate so no answer with undefined is guaranteed to fail on all implementations. Moreover a program which invokes undefined behavior is an erroneous program.

    But this one is the shortest I can get that segfault on my system:

    main(){main();}
    

    (I compile with gcc and -std=c89 -O0).

    And by the way, does this program really invokes undefined bevahior?

提交回复
热议问题