Thread: detect infinite loop in c program

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    detect infinite loop in c program

    hi
    please tellme the way i can detect an infinite loop in a c program

    regards
    abhi

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    Step through the code in a debugger.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf("If you see this infinite times, you have an infinite loop.\n");

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    detect infinite loop

    sorry i dont have the luxury to use the debugger.. i just have the c code with me and i can write one more code to do so....

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    well if you have a compiler, it will tell you for you when it will run infinately usually.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can also just step through it by hand. One way my professor taught us was to number each command and then on a piece of paper follow the logic of the program by writting each number down as it would be executed.

    Edit: yea it is long and boring, but hey thats programming

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    cmon guys, may be i was not able to explain the question properly. i was looking for an automated way of detecting infinite loop in a given code. The code will act as input to my detector. And i cant use debuggers or dont want to do it manually. I need to write a c program to do so...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i was looking for an automated way of detecting infinite loop in a given code.
    Forget it - this is known as the halting problem

    Unless of course you have to solve the halting problem for ONE specific program (which you seem to be keeping secret from the rest of us), then perhaps there is ONE specific program which can be written to decide the answer for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    i know its called halting problem ( when i searched on net) and the program can be anything, i mean no specific pattern for it... otherwise i wud have written a parser. ... but i also got to know there is some solution for the halting problem.... i was looking for that actually....

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but i also got to know there is some solution for the halting problem.... i was looking for that actually....
    How nice for you - now go read that link I posted and get a clue as to what the problem actually is, and not just knowing its name.

    Oh look, here's the first line.
    Alan Turing proved in 1936 that there is no general method or algorithm which can solve the halting problem for all possible inputs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    And i cant use debuggers or dont want to do it manually.
    *cough*lazy*cough*

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's not that simple Thantos - what the OP is trying to do is decide the following

    Code:
    /* test.c */
    /* this is any random C program which you care to mention - perhaps even homework.c itself */
    #include <stdio.h>
    int main ( ) {
      while ( 1 ) printf( "loser\n" );
      return 0;
    }

    Code:
    /* homework.c */
    #include <stdio.h>
    int main ( ) {
      FILE *fp = fopen( "test.c", "r" );
      if ( program_halts(fp) ) {
        printf( "Program in test.c halts\n" );
      } else {
        printf( "Program in test.c loops forever\n" );
      }
      fclose( fp );
      return 0;
    }
    But he just doesn't grok what the halting problem is all about, and thinks that its easy to come up with the code to implement program_halts()



    http://dictionary.reference.com/search?q=grok
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    An important part of coding is debugging. If one is unwilling to do that then they are lazy, no other way around it.

    His orginal question is
    tellme the way i can detect an infinite loop in a c program
    which makes no mention of writing a program to do the work.

    My first response was before he even made mention of automating it.

    I still stand by my assessment that they are being lazy.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another way: If you're on *nix, kill the looping program with the appropriate signal to create a dump file, then view the result in gdb/dbx. That'll give you a stack dump and lots of other useful info, and maybe a clue as to where the problem is.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    ha guys, i am terribly disappointed. rather than giving a simple algorithm in which will be detecting any given c program for infinite loops, random solutions are given.

    cmon even the kid on other block will know bout the signal ( dump file) solution, the debugger solution.. but i was looking for an algorithm....

    n for the one who mentioned the halting problem, i agree with u frnd that it was proved that its unsolvable in 1936.. but there are things that have been chg later.. n obv. i came to this board after research on internet..

    nyways thanks for ur co-operation(?)

    feel like spamming me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Why does the loop in my program turn infinite?
    By DaniiChris in forum C Programming
    Replies: 6
    Last Post: 07-08-2008, 02:44 AM
  4. How to detect another program installing?
    By HyperCreep in forum Windows Programming
    Replies: 7
    Last Post: 10-29-2007, 09:02 AM
  5. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM