+ 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
22 ответов
+ 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.
+ 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.
+ 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.
+ 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.
+ 3
Calvin,
I'm not sure, but in this case it isn't happening.
+ 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.
+ 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.
+ 2
Arsenic,
Thanks for informing me 🙏
I didn't know about that behaviour of initializer list. Learn something new again.
+ 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.
+ 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
+ 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.
+ 1
Arsenic Thank you.
0
Vasiliy but why? how do you know if a[6] is even or odd
0
Vasiliy no, a[6] is not necessarily equal 0
- 1
This loop will do three iterations
- 2
a[4] = 3
a[6] = 0 —> 0%2 = 0 —> 0 = false
cycle stopped 😉
- 2
what does "optional" mean?
In this example, “a“ with index “6“ does not exist, that is, it is equal to zero
- 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 😉