Why can't I cause a seg fault?

前端 未结 10 1738
名媛妹妹
名媛妹妹 2020-12-31 00:24

OK for whatever reason I\'m having trouble causing a seg fault. I want to produce one so that I can use gdb to see how to debug one. I have tried both examples

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 00:59

    The Wikipedia article actually lists three methods (one of which is a null pointer dereference); why didn't you try that one?

    As for why the two examples you tried didn't, well, the correct answer as others have noted is that it's undefined behavior, so anything could happen (which includes not segfaulting). But one could speculate that the reason that the first form didn't fail for you is because your compiler or system is lax about memory protection. As for the second case, a tail-recursive-aware compiler conceivably could optimize the infinitely recursive main loop and not end up overflowing the stack.

提交回复
热议问题