Branching Around Code
The following example contains a logic error where the program dereferences a null pointer:
1 int check_for_error (int *error_ptr)
2 {
3 *error_ptr = global_error;
4 global_error = 0;
5 return (global_error != 0);
6 }
The error occurs because the routine that calls this function assumes that the value of error_ptr can be 0. The check_for_error() function, however, assumes that error_ptr isn’t null, which means that line 3 can dereference a null pointer.
You can correct this error by setting an eval point on line 3 and entering:
if (error_ptr == 0) goto 4;
If the value of error_ptr is null, line 3 isn’t executed. Note that you are not naming a label used in your program. Instead, you are naming one of the line numbers generated by TotalView.
Adding a Function Call
The example in the previous section routed around the problem. If all you wanted to do was monitor the value of the global_error variable, you can add a printf() function call that displays its value. For example, the following might be the eval point to add to line 4:
printf ("global_error is %d\n", global_error);
TotalView executes this code fragment before the code on line 4; that is, this line executes before global_error is set to 0.
Correcting Code
The following example contains a coding error: the function returns the maximum value instead of the minimum value:
1 int minimum (int a, int b)
2 {
3 int result; /* Return the minimum */
4 if (a < b)
5 result = b;
6 else
7 result = a;
8 return (result);
9 }
Correct this error by adding the following code to an eval point at line 4:
if (a < b) goto 7; else goto 5;
This effectively replaces the if statement on line 4 with the code in the eval point.