I want this to crash, but it doesn't

前端 未结 4 1194
不知归路
不知归路 2021-01-17 01:06

seriously.. I\'m an old hacker from the 80\'s and with some spare time on my hands thought I\'d \'C\' what the fuss is about

( my background is Forth, which does thi

4条回答
  •  -上瘾入骨i
    2021-01-17 01:53

    C doesn't do any bounds checking. (Forth doesn't either, so I'm not sure where the expectation came from.)

    Overflowing an array is undefined behavior: it is allowed, but not required, to crash. In this case, the bytes just happen to be in the same virtual memory page as the stack frame where the local variable was placed. If the frame were towards the end of the page, the CPU would recognize a bad address and complain about the overflow.

    If you go a few kilobytes or megabytes out, you will likely see something like you expect.

提交回复
热议问题