Luck - ahthough technically speaking it is flawed - you are overwriting space that does not belong to "array a" - and in this case you have got away with it.
Here is a simple program to illustrate my point.
I have two other arrays C and B - these are initialised to zero's.
Variable r decides how many repateing values we write to array A.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
int c [50]={0};
int a[10];
int b[25]={0};
int r = 20; //increase this value to write more valuses to array A
int main() {
int i;
for (i = 0; i < r; i++)
{
a[i]=1;
}
cout << "Here are all the array(s) elements" << endl;
cout << "Array A" << endl;
for (i = 0; i < r; i++)
cout << a[i] << " ";
cout << endl << endl;
cout << "Array C" << endl;
for (i = 0; i < 50; i++)
cout << c[i] << " ";
cout << endl << endl;
cout << "Array B" << endl;
for (i = 0; i < 25; i++)
cout << b[i] << " ";
cout << endl;
system("pause");
return 0;
}
| |
This is the output of the program when I stay WITHIN the bounds of array A (all values correct)
Array A
1 1 1 1 1 1 1 1 1 1
Array C
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Array B
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Press any key to continue . . .
**Now here is what happens when I write 50 numbers to array A (well beyond it's bounds)**
Here are all the array(s) elements
Array A
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
Array C
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0
Array B
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Press any key to continue . . .
As you can see we have started to overwrite array C.
If we continue even further past the bounds of array A we will completely overwrite C and possibly even B