OK, It Compiles, Now What?
One is to use probes -- simple printf statements inserted to print useful information about the state of your program as it runs. Probes - printf() statementsLots and lots of printf() statements. Print out every variable you pass in to a function every variable that changes, and every variable that comes out. Print out your constants, and the result of every computation. Then when you run your program, look at all the values of all the variables and see if you can figure out what's going wrong. Statements like
Because output to stdout is buffered, printf() statements must contain a newline character or they will not always give you an accurate picture of your program execution if the program stops suddenly, as in a segmentation fault or bus error. Conditional probesI'm sure you've experienced this:
(1) One solution is to comment out the probes and only delete them when you freeze the code. (2) Another solution is to put the probes inside a condition, e.g.:
If you *think* your code is ok, you can define DBUG to be 0.
(3) A third approach is to use C's conditional compilation facility.
CSEE | 201 | 201 S'05 | lectures | news | resources | help |
|