Why are values in the array printed even though I passed an iterator bigger than the array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

2nd May 2018, 12:47 PM
Jonathan Perez
Jonathan Perez - avatar
1 Answer
+ 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.
4th May 2018, 5:06 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar