Exceeding array bound in C — Why does this NOT crash?

后端 未结 6 1328
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 15:25

I have this piece of code, and it runs perfectly fine, and I don\'t why:

int main(){
   int len = 10;
   char arr[len];
   arr[150] = \'x\';
}
6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 16:20

    Because you were lucky. Or rather unlucky, because it means it's harder to find the bug.

    The runtime will only crash if you start using the memory of another process (or in some cases unallocated memory). Your application is given a certain amount of memory when it opens, which in this case is enough, and you can mess about in your own memory as much as you like, but you'll give yourself a nightmare of a debugging job.

提交回复
热议问题