Array overflow (why does this work?)

前端 未结 5 1028
暖寄归人
暖寄归人 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:03

    It's undefined behavior. UB comes in many flavors. Here are a few:

    1) It will kick your dog.

    2) It will reformat your hard drive.

    3) It will work without a problem.

    In your case, with your compiler and on your platform and on this particular day, you are seeing (3). But try it elsewhere, and you might get (1), (2), or something else completely (most likely an access violation).

提交回复
热议问题