Can a C/C++ program seg-fault from reading past the end of an array (UNIX)?

后端 未结 3 579
野趣味
野趣味 2021-01-18 02:33

I\'m aware that you can read past the end of an array - I\'m wondering now if you can seg-fault just by performing that reading operation though.

int someint         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-18 03:00

    The second line can cause literally anything to happen and still be correct as far as the language specification is concerned. It could print gibberish, it could crash due to a segmentation fault or something else, it could cause power to go out on the entire eastern seaboard, or it could cause the canonical demons to fly out of your nose...

    That's the magic of undefined behaviour.

提交回复
热议问题