Tuesday 8 November 2011

My program hangs when I run it. What should I do? in C programming

My program hangs when I run it. What should I do?

There are many reasons a program might stop working when you run it. These reasons fall into four basic categories:

The program is in an infinite loop.
The program is taking longer than expected.
The program is waiting for input from some source and will not continue until
that input is correctly entered.
The program was designed to delay for an unspecified period or to halt, execution.

An examination of each of these situations will follow, after a discussion of the techniques for debugging
programs that hang for no apparent reason.

Debugging programs that hang for no reason can be particularly difficult. You might spend hours carefully
crafting a program, trying to assure yourself that all the code is exactly as intended, or you might make one
tiny modification to an existing program that had previously worked perfectly. In either case, you run the
program and are rewarded with an empty screen. If you had gotten erroneous results, or even partial results,
you would have something to work with. A blank screen is frustrating. You don’t even know what went
wrong.

To begin debugging a program like this, you should look at the listing and assure yourself that each section
of the program, in the order in which the sections should be executed, is working properly. For example, say
that the main program consists only of calls to three functions. We’ll call them functions A(), B(), and C().
Start by verifying that function A() returns control to the main program. You can do this by placing an exit()
command directly after the call to function A(), or by commenting out the calls to functions B() and C().
You then can recompile and rerun the program.

NOTE
This action could, of course, be carried out just as well with a debugger; however, this answer
illustrates the classical approach to debugging. A debugger is a program that enables the
programmer to observe the execution of his program, the current line it is on, the value of variables,
and so forth.

What this will show is whether function A() ever returns control to the main program. If the program runs
and exits, you will know that it is another part of the program that is hanging up. You can continue to test
all the routines in this way until you discover the guilty routine. Then you can focus your attention on the
offending function.

Sometimes the situation will be somewhat more complex. For example, the function in which your program
hangs might be perfectly OK. The problem might be that that function is getting erroneous data from
somewhere else. In this case, you will need to monitor the values your function is accepting and observe which

ones are causing the undesired behavior.
TIP
Monitoring functions is an excellent use for a debugger.
An examination of a sample program will help illustrate the use of this technique:
#include <stdio.h>
#include <stdlib.h>
/*
* Declare the functions that the main function is using
*/
int A() , B( int ) , C( int , int );
/*
* The main program
*/
int A() , B() , C(); /* These are functions in some other
module */
int main()
{
int v1 , v2 , v3;
v1 = A();
v2 = B( v1 );
v3 = C( v1 , v2 );
printf( “The Result is %d.\n” , v3 );
return(0);
}

After the line that invokes function A(), you can print the value of the variable v1 to assure yourself that it
is within the range of values the function B() will accept. Even if function B() is the one taking forever to execute,

it might not be the erroneous function; rather, function A() might be giving B() values that it never expected.
Now that you’ve examined the method of debugging programs that simply “hang,” it’s time to look at some
of the common errors that cause a program to hang.

No comments:

Post a Comment