0

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?

Alok Save
  • 202,538
  • 53
  • 430
  • 533
Shibli
  • 5,879
  • 13
  • 62
  • 126
  • Google for "fandango on core" – Eugen Rieck Jan 07 '12 at 15:36
  • 1
    This is, of course, impossible. Values in an array do not get modified unless you write code to modify them. Post the rest of your code and do tell us what language you're working in. – Cody Gray - on strike Jan 07 '12 at 15:41
  • without initialization (or otherwise setting its values) you really shouldn't examine the contents of the array. It is [Undefined Behaviour](http://c2.com/cgi/wiki?UndefinedBehavior) to do so. – pmg Jan 07 '12 at 15:48

1 Answers1

3

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.
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 2
    @OliCharlesworth: Correction, *"Magic" is a good answer to all SO questions which don't show any code*. :) – Alok Save Jan 07 '12 at 17:18