preda2or's blog

By preda2or, history, 6 years ago, In English

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).

Full text and comments »

  • Vote: I like it
  • +11
  • Vote: I do not like it