Why doesn't my program crash when I write past the end of an array?

前端 未结 9 1250
-上瘾入骨i
-上瘾入骨i 2020-11-22 12:47

Why does the code below work without any crash @ runtime ?

And also the size is completely dependent on machine/platform/compiler!!. I can even give upto 200 in a 64

9条回答
  •  忘掉有多难
    2020-11-22 12:55

    A segmentation fault occurs when a process tries to overwrite a page in memory which it doesn't own; Unless you run a long way over the end of you're buffer you aren't going to trigger a seg fault.

    The stack is located somewhere in one of the blocks of memory owned by your application. In this instance you have just been lucky if you haven't overwritten something important. You have overwritten perhaps some unused memory. If you were a bit more unlucky you might have overwritten the stack frame of another function on the stack.

提交回复
热议问题