Could accessing array element outside boundary corrupt it

Viewed 154

Is it possible that accessing an array outside its boundary corrupt its existing elements

MyObject* array[10];

for(int i=0; i<10; i++)
{
    array[i] = nullptr;
}
array[1] = new MyObject();
array[8] = new MyObject();
array[15] = new MyObject();

could accessing array[15] result in the corruption of the elements between 0-10?

1 Answers

could accessing array[15] result in the corruption of the elements between 0-10?

Yes. This is undefined behavior, and the nature of UB is that anything can happen. In particular, it usually doesn't make much sense to reason about what could happen or what is more likely to happen. It can be anything, including the corruption of some array elements that were intact before UB.

Related