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

前端 未结 9 1203
-上瘾入骨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:11

    By using an array type, which C++ has inherited from C, you are implicitly asking not to have a range check.

    If you try this instead

    void main(int argc, char* argv[])
    {     
        std::vector arr(3);
    
        arr.at(4) = 99;
    } 
    

    you will get an exception thrown.

    So C++ offers both a checked and an unchecked interface. It is up to you to select the one you want to use.

提交回复
热议问题