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

前端 未结 9 1176
-上瘾入骨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 13:00

    Your code has Undefined Behavior. That means it can do anything or nothing. Depending on your compiler and OS etc., it could crash.

    That said, with many if not most compilers your code will not even compile.

    That's because you have void main, while both the C standard and the C++ standard requires int main.

    About the only compiler that's happy with void main is Microsoft’s, Visual C++.

    That's a compiler defect, but since Microsoft has lots of example documentation and even code generation tools that generate void main, they will likely never fix it. However, consider that writing Microsoft-specific void main is one character more to type than standard int main. So why not go with the standards?

    Cheers & hth.,

提交回复
热议问题