hi
please tellme the way i can detect an infinite loop in a c program
regards
abhi
hi
please tellme the way i can detect an infinite loop in a c program
regards
abhi
Step through the code in a debugger.
printf("If you see this infinite times, you have an infinite loop.\n");
Quzah.
Hope is the first step on the road to disappointment.
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....
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
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
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...
> 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.
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....
> 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.
*cough*lazy*cough*And i cant use debuggers or dont want to do it manually.
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; }
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()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
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.
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 iswhich makes no mention of writing a program to do the work.tellme the way i can detect an infinite loop in a c program
My first response was before he even made mention of automating it.
I still stand by my assessment that they are being lazy.
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]
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