Debugging Techniques
Use of the Assert Macro
Another approach to debugging is the use of the assert macro. This allows
checking for
assumptions at specific points of the program. It is useful at
determining
invalid input, or unexpected values. The following example, although very
simple,
illustrates the use of this debugging method...
Source Code Before:
#include <stdio.h>
int GetVolume(int width, int length, int height);
int main ()
{
int length, width, height;
int volume;
printf("Enter the length, width & height\n");
scanf("%d %d %d", &length, &width, &height);
volume = GetVolume(length, width, height);
printf("The volume is %d units cubed\n", volume);
return 0;
}
int GetVolume(int length, int width, int height)
{
return(length * width * height);
}
|
Compilation & Execution Before:
linux1[58]% gcc -ansi -Wall volume.c
linux1[59]% a.out
Enter the length, width & height
3 3 -3
The volume is -27 units cubed
linux1[60]%
|
In the above example the user entered a negative value for the height.
This is an invalid
measurementand in return an invalid result is calculated for the volume.
It would
be good to check if all of he measurements passed into
"getVolume()"
were valid. This is how to do so...
|
Source Code After:
#include <stdio.h>
#include <assert.h>
int GetVolume(int width, int length, int height);
int main ()
{
int length, width, height;
int volume;
printf("Enter the length, width & height\n");
scanf("%d %d %d", &length, &width, &height);
volume = GetVolume(length, width, height);
printf("The volume is %d units cubed\n", volume);
return 0;
}
int GetVolume(int length, int width, int height)
{
assert(length > 0 );
assert(width > 0 );
assert(height > 0 );
return(length * width * height);
}
|
Compilation & Execution After:
linux1[63]% gcc -ansi -Wall volume.c
linux1[64]% a.out
Enter the length, width & height
3 3 -3
a.out: volume.c:25: GetVolume: Assertion `height > 0' failed.
Abort (core dumped)
linux1[65]%
|
This example included a library called "assert.h".
This allows confirmation in the program by way of standard C syntax.
Just put a C
expression within
the "assert();" and if it fails, it will state where and
terminate the program. The
above example could have stated "assert(length > 0 && width > 0 &&
height > 0);".
However, by breaking the assert down into several
smaller parts it becomes easy to see which part failed.
When including this library enclose it with brackets
<assert.h>, not quotes.
|
Notes:
- No Unnecessary Clutter
- This macro is particularly clean, as it will only print a failed
assertion message and
break the program if something was wrong.
- Detailed Debugging Information
- Failed assertions print the line number of the failed assert and the
file that it
failed in (nice for long & multiple file compilations).
- For Debugging Use Only
- Asserts should never be left in the final code.
More graceful ways of handling invalid input should be developed.
Aborting a program abnormally, whether bus error, segmentation fault,
etc... is not an acceptable
programming practice.
Modified by:
Dawn Block,
September 2003
Created by:
Daniel J. Hood,
February 2000 http://www.csee.umbc.edu/~cshc/resources/debug/asserts.shtml
|
|
|