I've been trying to work out why my array is printing out -858993460 with the following code
int myInt = 0;
int myArray[10]; // I know this is undefined, but the same results occur if I put {} to initialize it
while(myInt <= 10)
{
myArray[myInt] = myInt;
myInt++;
std::cout << "Test " << myArray[myInt] << std::endl;
std::cout << "Counter " << myInt << std::endl;
}
When print this out, myInt
increments fine. However, myArray
is printing out -858993460. If I insert a break point and step over the while loop with each iteration, I can see the numbers are being fed into the array, but it only prints out that random number (assuming it's a random number from the stack/heap?).
Now, if I swap around the while loop so it's now
while(myInt <= 10)
{
myInt++;
myArray[myInt] = myInt;
}
it prints out the numbers correctly. I can't seem to work out what's going on here...