Consider the following snippet
int x[] = {1,2,3};
cout << *x << endl; // 1
cout << *(x+1) << endl; // 2
cout << *(x-10) << endl; // Different number each time i run the program
Why is it that last cout consistently display a different number each time i run the compiled program? It is understood that result is unpredictable and is undefined, but i would imagine it should be consistent. Why does it change?
The memory block you are trying to access is not legally owned by your array and the program. That portion of memory would be owned by some other process going on. so every time it holds different value. It is also possible that you get the same answer some other time.