Search This Blog

Sunday, 20 October 2013

bugs and debugging

In general terms a bug is an error in a computer program that leads to unexpected and unwanted behavior. (Lore has it that the first “bug” was a burnt moth found in the relays of the early Mark I computer in the 1940s; however, as early as 1878 Thomas Edison had referred to “bugs” in the design of his new inventions.)

Computer bugs can be divided into two categories: syn-tax errors and logic errors. A syntax error results from failing to follow a language’s rules for constructing state-ments, or from using the wrong symbol. For example, each statement in the C language must end with a semicolon. This sort of syntax error is easily detected and reported by modern compilers, so fixing it is trivial.

A logic error, on the other hand, is a syntactically valid statement that does not do what was intended. For example, if a C programmer writes:

if Total = 100

instead of

if Total == 100

the programmer may have intended to test the value of Total to see if it is 100, but the first statement actually assigns the value of 100 to Total. That’s because a single equals sign in C is the assignment operator; testing for equality requires the double equals sign. Further, the error will result in the if statement always being true, because the truth value of an assignment is the value assigned (100 in this case) and any nonzero value is considered to be “true” (see branching statements).

Loops and pointers are frequent sources of logical errors (see loop and pointers and indirection). The boundary condition of a loop can be incorrectly specified (for exam-ple, < 10 when < = 10 is wanted). If a loop and a pointer or index variable are being used to retrieve data from an array, pointing beyond the end of the array will retrieve whatever data happens to be stored out there.

Errors can also be caused in the conversion of data of different types (see data types). For example, in many lan-guage implementations the compiler will automatically con-vert an integer value to floating point if it is to be assigned to a floating point variable. However, while an integer can retain at least nine decimal digits of precision, a float may only be able to guarantee seven. The result could be a loss of precision sufficient to render the program’s results unre-liable, particularly for scientific purposes.

Debugging Techniques

The process of debugging (identifying and fixing bugs) is aided by the debugging features integrated into most mod-ern programming environments. Some typical features include the ability to set a breakpoint or place in the code where the running program should halt so the values of key variables can be examined. A watch can be set on specified certain variables so their changing values will be displayed as the program executes. A trace highlights the source code to show what statements are being executed as the program

runs. (It can also be set to follow execution into and through any procedures or subroutines called by the main code.)

During the process of software development, debugging will usually proceed hand in hand with software testing. Indeed, the line between the two can be blurry. Essentially, debugging deals with fixing problems so that the program is doing what it intends to do, while testing determines whether the program’s performance adequately meets the needs and objectives of the end user.


No comments:

Post a Comment