Where do these numbers come from?

closed account (iNU7ko23)
// more pointers
#include <iostream>
using namespace std;

int main ()
{
int array[] = {5,2,8,4,9};
int counter;
for(counter = 0; counter < 10; counter++)
{
cout << array[counter] << endl;

}
system("PAUSE");
return 0;
}


After compiling this test of arrays after the five numbers 5 random numbers are printed. Where do these come from? Are they just randomly made up?
In this case, array[i] is shorthand for "the integer value stored in memory at location array + i", so whatever is at that memory location is read as an int, and given to you.

What's in that memory location? Well, you didn't put anything there, so it could be anything at all. It's not random; it's whatever was last put in that piece of memory.

If you ask for a memory location the OS didn't think was yours to look at, you'd get a segfault. As it is, this piece of memory is within the memory given to your program by the OS.

If you tried
cout << array[10000000]; you may well get a segFault.
Topic archived. No new replies allowed.