How to prevent Infinite loop in Sublime Text (Solution).

Revision en2, by preda2or, 2019-02-06 10:03:07

Many users who use sublime text to code in C++ might have faced the infinite loop problem in which the system freezes and the only way out is to restart the machine. If this happens during an ongoing contest it wastes a lot of time. I have a permanent fix for this problem which will safeguard the program from accidentally running into infinite loop.

Sublime C++14 build with timeout : LINK

You can test the above build by running this code.

#include <bits/stdc++.h>   
using namespace std;   

int main() {   
    ios::sync_with_stdio(0);   
    cin.tie(0);   
   
    #ifndef ONLINE_JUDGE   
    freopen("input.txt", "r", stdin);   
    freopen("output.txt", "w", stdout);   
    #endif   
    
    while (true) {   
        cout << "Hello World\n";   
    }   
        
}   

Alternative C++14 Build which will take input from input.txt and store output in output.txt files with timeout: LINK

You can test the above build by running this code.

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    while (true) {
        cout << "Hello World\n";
    }
    
}

This Sublime build will stop executing the program if it's execution time goes more than 0.5s, thus preventing the system freeze. Usually when we run a sample test in our local machine this limit of 0.5s is more than enough. You can set the timeout to a higher value. 0.5s works fine in my machine.

I hope some users might find this useful.

P.S — I have tried these builds in linux(Ubuntu).

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English preda2or 2019-02-06 10:03:07 0 (published)
en1 English preda2or 2019-02-06 10:01:00 1739 Initial revision (saved to drafts)