+ 2

Why this code causes an infinite loop? Why the output is like this? Is this an undefined behavior of compiler?

I think it's an undefined behavior of compiler, but my main question is why it won't happen with a of size 5 and 10 times iteration. I changed the size of a and iteration number, but with this specific numbers causes infinite loop. https://code.sololearn.com/cUH9r4tcAwMX/?ref=app

1st Sep 2018, 9:57 AM
shiva
shiva - avatar
5 Answers
+ 2
Anna is 100% correct here, in your code if you set your size to half of your loop and a[i] = also to half your loop it will work fine because when you start your program you make an array with 10 elements and it will give this a memory location i got 0x4d2040 and the last element will get 0x4d2064 and then you have a int i and it will give this variable the next memory address of 0x4d2068 so as your go threw your loop its going to use those memory addresses, but your loop goes past 10 to when your loop is at ten its setting memory address 0x4d2064 to 5 next loop its going to add 4 bytes to this and set 0x4d2068 to 5 now thats the memory address of i so its setting i to 5 now and you will now have a loop from a[5] to a[9] because every time it hits a[10] that doesnt exist its changing the memory address for i. now if you have 5 and 10 this doesnt matter becasue you are changing the a[5] that doesnt exsist which is the i memory location to 5 but its already suppose to be 5 so the loop goes on past i’s memory location. to see this in action you can add cout << &i << " " << &a[i] << endl; right before a[i] = 5 and it will show you that when you get the a[10] the memory addresses are the same. hope this wasnt to confusing.
1st Sep 2018, 4:13 PM
Mooaholic
Mooaholic - avatar
+ 6
shiva if something is undefined behavior, you cannot get to know what and why it happens.... array is contiguous memory location and for the case whatever you mentioned , a as 5 and loop 10 times, it might be possible that array allocated was sufficient enough to handle 9 inputs... but one should not rely on this behavior as it is undefined.... proof is the minor change and you are gone again as below : for a size as 5 and loop as 10 times, you are getting non infinite loop.. just change a[i] =5 to 4 and again it becomes infinite loop... to conclude, it's undefined behavior and as per it's name, don't get into logic of same as you cannot define (undefined)
1st Sep 2018, 10:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 6
Note that variables are only pointers to certain locations in memory. With the declaration int a[10], you reserve memory for 10 integers. Let's say they are stored in memory locations #0-#9. With int i, you reserve memory for another integer. It'll probably be stored in the next available memory location, #10 in this case. Your for loop will first iterate over the values that are stored in the memory locations #0-#9 (your array a[]). But it won't stop there and iterate over more consequent memory locations that don't belong to a[] anymore. Unlike most other programming languages, C++ doesn't stop you from doing that. So once it is done with a[9], your loop will set i to 5 in the next step. My theory is that it'll then jump back six memory locations to where a[4] is stored, continue the loop from there until it reaches the end of a[] again, then it will reset i to 5 and you're caught in an endless loop. I don't know if this is 100% accurate, but considering the fact that C++ allows you to access a[10] (the 11th element) of an array that only has 10 elements, it seems logical. If you declare an array a with a size of 5, you won't get into an endless loop because at the 6th iteration, i will be set to 5 which will be its value anyway. If you set it to 4, there will be an endless loop again (because you jump one position back, one position forward, one position back etc.). Same with int a[10] and a loop length of 20: If you set a[i] to 10 (instead of 5), there will be no endless loop.
1st Sep 2018, 11:36 AM
Anna
Anna - avatar
+ 2
shiva you should have array of same size... check with int a[20]; instead of size 10
1st Sep 2018, 10:15 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Ketan Lalcheta I know that, I made my question and description more precise, I hope you can answer it. sorry for ambiguity of my question.
1st Sep 2018, 10:20 AM
shiva
shiva - avatar