Array overflow (why does this work?)

前端 未结 5 1034
暖寄归人
暖寄归人 2020-12-06 03:23

Okay, so I was teaching my girlfriend some c++, and she wrote a program that I thought wouldn\'t work, but it did. It accesses one more element in the array then there is (f

5条回答
  •  青春惊慌失措
    2020-12-06 04:05

    C/C++ does not do boundary checking when using arrays.

    Since you are declaring a stack based array. Accessing outside the bounds of the array will just access another part of already allocated stack space.

    So basically when you access something that is out of bounds, it won't throw a segmentation fault unless its completely out of your stack memory.

    C/C++ is dangerous with array boundaries remember that!

提交回复
热议问题