C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-29-2003, 12:48 AM   #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
abhivyakat is offline   Reply With Quote
Old 09-29-2003, 01:35 AM   #2
Rog
Registered User
 
Join Date: Jan 2003
Posts: 78
Step through the code in a debugger.
Rog is offline   Reply With Quote
Old 09-29-2003, 02:20 AM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,292
printf("If you see this infinite times, you have an infinite loop.\n");

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 09-29-2003, 06:43 AM   #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....
abhivyakat is offline   Reply With Quote
Old 09-29-2003, 07:31 AM   #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
chrismiceli is offline   Reply With Quote
Old 09-29-2003, 07:43 AM   #6
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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
Thantos is offline   Reply With Quote
Old 09-30-2003, 12:22 AM   #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...
abhivyakat is offline   Reply With Quote
Old 09-30-2003, 01:34 AM   #8
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
> 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.

Salem is offline   Reply With Quote
Old 09-30-2003, 03:13 AM   #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....
abhivyakat is offline   Reply With Quote
Old 09-30-2003, 04:53 AM   #10
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
> 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.
Quote:
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.

Salem is offline   Reply With Quote
Old 09-30-2003, 10:41 AM   #11
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Quote:
And i cant use debuggers or dont want to do it manually.
*cough*lazy*cough*
Thantos is offline   Reply With Quote
Old 09-30-2003, 11:11 AM   #12
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
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.

Salem is offline   Reply With Quote
Old 09-30-2003, 11:24 AM   #13
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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
Quote:
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.
Thantos is offline   Reply With Quote
Old 09-30-2003, 01:03 PM   #14
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
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]
Hammer is offline   Reply With Quote
Old 09-30-2003, 11:09 PM   #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
abhivyakat is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22