简体   繁体   English

可以访问边界外的数组元素来破坏它

[英]Could accessing array element outside boundary corrupt it

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? 可以访问数组[15]导致0-10之间的元素损坏?

could accessing array[15] result in the corruption of the elements between 0-10? 可以访问array[15]导致0-10之间的元素损坏?

Yes. 是。 This is undefined behavior, and the nature of UB is that anything can happen. 这是未定义的行为,UB的本质是任何事情都可能发生。 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. 它可以是任何东西,包括在UB之前完整的某些数组元素的损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM