0
Why are values in the array printed even though I passed an iterator bigger than the array?
int main() { int b[] = {11, 45, 62, 70, 88}; for (int i = 0; i < 5; i++){ cout << b[i] << endl; } } //output 11, 45, 62, 70, 88 that makes sense but if I try.. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< int main() { int b[] = {11, 45, 62, 70, 88}; for (int i = 0; i < 10; i++){ cout << b[i] << endl; } } //output 11, 45, 62, 70, 88, 0, 8, 7..... where did the other values come from?
1 Resposta
+ 2
You are accessing unused memory. luckily you are getting because that memory is free otherwise you will get segmentation fault. Try a loop for 1k iteration and check.