is this code an infinite loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

is this code an infinite loop?

is this code an infinite loop or not? int main() { int a[6] = {1, 9, 7, 5, 3, 8}; for (int i = 0; a[i] % 2; i += 2) { cout << a[i] << endl; } } at the first look this looks like an infinite loop since I didn't use i < 6 or break, so i will be 0 2 4 6 8... but we can use a[12] and a[14] now can't we? and that's give us whatever left in that address in RAM what if that number is even, in other words a[i] % 2 == 0 and the loop ends

5th Jun 2021, 3:20 PM
Alpha Zero
Alpha Zero - avatar
22 Answers
+ 6
This is neither an infinite loop, nor a finite loop. It's a mess Accessing array elements beyond it's bounds is what known as Undefined behaviour, and it is useless to predict anything till the time there is an UB in your program.
5th Jun 2021, 3:47 PM
Arsenic
Arsenic - avatar
+ 5
Ipang it is not compiler dependent behaviour. According to standards, when using an initializer list to initialize array, All array elements that are not initialized explicitly are zero-initialized. see this for info 👇 https://en.cppreference.com/w/c/language/array_initialization P.S. again this doesn't account for elements beyond the bound of array.
5th Jun 2021, 4:18 PM
Arsenic
Arsenic - avatar
+ 4
Calvin Thomas it is not always the case that the array will be right at the end of the stack memory allocated to the program. If that's the case and array pointer tries to acess memory location that doesn't belong to your process, it's the OS which generates the segmentation fault.
6th Jun 2021, 1:47 AM
Arsenic
Arsenic - avatar
+ 3
It could be, and it could be not. It depends on what value lies ahead, whether it was odd/even, I think. Ran that code several times in curiosity, and during the test runs I found that the next value after the last one to be even, which stopped the loop. But I'm not sure whether this was a sheer luck or what. Also tried sizing by 12 & 14 and it behaved the same, the value past the last one was garbage, different on each run, but appears to be constantly an even value. Again, I wouldn't count on this being a reliable stuff. I think Arsenic has a good point here.
5th Jun 2021, 3:52 PM
Ipang
+ 3
Arsenic and Ipang Wouldn't this code raise a Segmentation Error as the pointer will be incremented to a value above the stack's range?
5th Jun 2021, 4:39 PM
Calvin Thomas
Calvin Thomas - avatar
+ 3
Calvin, I'm not sure, but in this case it isn't happening.
5th Jun 2021, 10:47 PM
Ipang
+ 2
Ipang if you try a bigger size array and like 12,14 etc then then the loop is bound to break after third iteration as then rest all the values are by default initialised to zero.
5th Jun 2021, 3:58 PM
Arsenic
Arsenic - avatar
+ 2
Arsenic, I think most, but not all compiler initialize array elements by zero, I don't feel too safe relying on that policy especially about local variables. During the test runs, I also saw non zeroes after the last one, they are garbage, sometimes positive, some other times negative. But the thing I don't quite understand, they are constantly being an even value.
5th Jun 2021, 4:10 PM
Ipang
+ 2
Arsenic, Thanks for informing me 🙏 I didn't know about that behaviour of initializer list. Learn something new again.
5th Jun 2021, 4:26 PM
Ipang
+ 2
Calvin Thomas that excess space is to keep information about active subroutines ( including register information, adress to where the function should return after execution, it's local variables etc ) in your program. On every function call during your program execution, a new stack frame is created on top of the stack with the required information and destroyed as soon as soon as it finishes execution.
6th Jun 2021, 4:29 AM
Arsenic
Arsenic - avatar
+ 1
Ipang exactly but when I run this on SoloLearn it gives me no output which is the output when there's infinite loop and that's confused me
5th Jun 2021, 3:56 PM
Alpha Zero
Alpha Zero - avatar
+ 1
Alpha Zero, In that case, it is as I said before, that it could be or could be not an infinite loop. SoloLearn stops a program when it runs too long (as in an infinite loop). I'm guessing the "No output" came out cause it was running out of plan.
5th Jun 2021, 4:14 PM
Ipang
+ 1
Arsenic and Ipang Oh, I see. Thank you for the response. So a portion of the stack allocated to the process by the OS is unused, as all the variables has their values stored in a top-to-down approach, right? Why should the OS do this?
6th Jun 2021, 2:40 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Arsenic Thank you.
7th Jun 2021, 4:36 AM
Calvin Thomas
Calvin Thomas - avatar
0
Vasiliy but why? how do you know if a[6] is even or odd
5th Jun 2021, 3:53 PM
Alpha Zero
Alpha Zero - avatar
0
Vasiliy no, a[6] is not necessarily equal 0
5th Jun 2021, 4:04 PM
Alpha Zero
Alpha Zero - avatar
- 1
This loop will do three iterations
5th Jun 2021, 3:50 PM
Solo
Solo - avatar
- 2
a[4] = 3 a[6] = 0 —> 0%2 = 0 —> 0 = false cycle stopped 😉
5th Jun 2021, 4:02 PM
Solo
Solo - avatar
- 2
what does "optional" mean? In this example, “a“ with index “6“ does not exist, that is, it is equal to zero
5th Jun 2021, 4:09 PM
Solo
Solo - avatar
- 2
Knowledge of higher mathematics is not needed here to understand that this cycle will never be infinite, since there is always a zero value in the heap in which the array is stored, which is why it is a heap 😉
5th Jun 2021, 6:03 PM
Solo
Solo - avatar