Accessing unallocated memory C++

前端 未结 6 1790
独厮守ぢ
独厮守ぢ 2020-12-11 20:06

I am having this piece of code:

try
{
    int* myTestArray = new int[2];

    myTestArray[4] = 54;

    cout << \"Should throw ex \"  << myTestAr         


        
6条回答
  •  Happy的楠姐
    2020-12-11 20:27

    You can write out of array range, but it is not guaranteed to work, and the data is not guaranteed to be persistent there, as something else can overwrite it.

    It's simply not a good idea, and since there's no exception, potentially hard to find bug.

    When reading that memory, you will be pulling some random garbage that was there left over from some other program or whatever used the memory before, so it can really be anything.

提交回复
热议问题