Why can't I cause a seg fault?

前端 未结 10 1746
名媛妹妹
名媛妹妹 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条回答
  •  旧巷少年郎
    2020-12-31 00:59

    For the first example, the compiler probably put the string in writable memory, so there is no seg fault when trying to change it.

    For the second, the compiler may be optimizing the call away or may be optimizing the call itself away to just a jump (since it is a tail call), meaning the stack isn't actually growing with return addresses for each call, so you could recurse indefinitely.

    But as Neil mentioned in his post, any of these things results in "undefined behavior", so the code is not required at runtime to generate a seg fault.

提交回复
热议问题