Home > OS >  why infinite loop terminates? or go infinite
why infinite loop terminates? or go infinite

Time:02-24

I was trying a test and I wrote this program...

#include<iostream>
using namespace std;
main()
{
    int arr[5]={1,2,3,5,3}, num=5;
    for(int i=0; i< num; i  )
    {
        for(int j=(i 1); i< num; j  )
        {
            if (arr[i]==arr[j])
            {
                cout<<"test";
            }
            cout<<j<<endl;
        }
    }
}

hmmm... In the below code I know I used "i" instead of "j",So just to create the problem I used this

for(int j=(i 1); i< num; j  )

And my problematic part of the program is-

for(int j=(i 1); i< num; j  )
        {
            if (arr[i]==arr[j])
            {
                cout<<"test";
            }
            cout<<j<<endl;
        }

in the above code when I remove this if block-

if (arr[i]==arr[j])
            {
                cout<<"test";
            }

the program goes infinite......... But when I put this if block again the program terminates automatically after some execution. I also read about this on this link but it shows example of java

And there they show the reason of negative value, but in my program there is no garbage value comes.

CodePudding user response:

The loop condition is never false. Hence the program will continue running indefinitely.

However, with if (arr[i]==arr[j]) you access outside the bounds of arr. Hence the behaviour of the program is undefined. When the behaviour of the program is undefined, it doesn't necessarily behave as you might expect. For example, it might not continue running indefinitely.

Even without if (arr[i]==arr[j]), the loop will eventually have undefined behaviour when the loop counter overflows. But a program with undefined behaviour doesn't necessarily behave as you might not expect. For example, it might continue running indefinitely.

CodePudding user response:

Without the if (arr[i]==arr[j]), you simply have an infinite loop, which is perfectly valid in this case. j will just keep getting incremented (eventually this will overflow, which in undefined behaviour, see [basic.fundamental]/2), and the condition will never be met, since i does not chance.

However, with the if (arr[i]==arr[j]) in the code, j is being incremented, and you will go outside the bounds of the array (as you know). In C , this is Undefined Behaviour, meaning anything can happen. In your case, it seems to be causing the program to crash (which is actually a good thing, since it indicates an error).

CodePudding user response:

I think, the problematic part of code is

for(int j=(i 1); i< num; j  )

You are increasing j while you are checking i< num.

If you replace it with

for(int j=(i 1); j < num; j  )

it might start working.

Why it is terminating with if block, and not terminating without if block

if (arr[i]==arr[j])
{
  cout<<"test";
}

Because int have finite amount of possible values. When you reach the maximal value of int variable, the value 1 is the smallest possible int. (As @user17732522 pointed out, the behavior might be utterly different - program crashing or something nasty happening here - because behavior of int 'overflowing' is undefined. Therefore your programs memory might be already corrupted here and it need to perform some operations to see it happened...eg. by program crash.)

Now, let's think about, what arr[i] does. arr is basically a pointer to a memory, where array begins and [i] operation is the same thing as *(arr i).

Now, there are two possibilities what might happen

  • Program crashes when i is positive and outside of the array
  • Program crashes when i is negative

In both cases, the program crashes, because you are trying to access protected or nonexistent zone of a memory. This depends on the architecture of the system - Eg. In negative zone on 64 bit AMD64 system, you are clearly accessing nonexistent memory, if you don't have 16 exabytes of RAM. The program must in such case cause processor interruption, which is given to your OS, which kills your application.

Why it doesn't crashes when you delete the block? Nobody is trying to access the *(arr j) and therefore, j is repeating from minimal possible value to maximal possible value without any problem.

CodePudding user response:

The problem is this if statement will always be true:

for(int j=(i 1); i< num; j  ){infinity}

Try setting it to this instead:

for(int j=(i 1); j< num; j  ){not infinity}

You can't pass a loop on a statement that will not eventually return false or it'll go on forever.

  •  Tags:  
  • c
  • Related