Value of array member changes illogically

和自甴很熟 提交于 2019-12-25 04:20:55

问题


I noticed once that when I declare an array,

int arr[10];

after a while the value of array member changes although nothing affects it during a period. Then I made use of dynamic allocation with "new" and the problem solved. I thought that everything should be declared dynamically. But this shouldnt be true. What may be a logical reason?


回答1:


Could be number of reasons:

  • Initialize your array member if it is on local storage or it contains any random value if you didn't.
  • You probably overwrite the bounds of some other array in the same function which corrupts the stack and your array.
  • You corrupt the stack in some magical way.


来源:https://stackoverflow.com/questions/8770783/value-of-array-member-changes-illogically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!