![]() |
| | #1 |
| Registered User Join Date: Sep 2003
Posts: 6
| detect infinite loop in c program please tellme the way i can detect an infinite loop in a c program regards abhi |
| abhivyakat is offline | |
| | #2 |
| Registered User Join Date: Jan 2003
Posts: 78
| Step through the code in a debugger. |
| Rog is offline | |
| | #3 |
| +++ OK NO CARRIER 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 | |
| | #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 | |
| | #5 |
| Obsessed with C 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 | |
| | #6 |
| & the hat of GPL slaying 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 | |
| | #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 | |
| | #8 |
| and the hat of Destiny 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. |
| Salem is offline | |
| | #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 | |
| | #10 | |
| and the hat of Destiny 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:
| |
| Salem is offline | |
| | #11 | |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| Quote:
| |
| Thantos is offline | |
| | #12 |
| and the hat of Destiny 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;
}
http://dictionary.reference.com/search?q=grok |
| Salem is offline | |
| | #13 | |
| & the hat of GPL slaying 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:
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 | |
| | #14 |
| End Of Line 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |