とらりもんHOME  Index  Search  Changes  Login

C intro 8. debug

What is a bug?

A mistake in the program is called bug in the IT community. Its original meaning is a bad insect. It is like there is an insect inside a machine and it makes bad things and prevent our program work smoothly (although the bug is made by ourselves). The process of deleting the bugs, which is deleting the mistakes and fix the program, is called debugging (or "bug fix") in the IT community.

There are many causes of bugs. In particular,

  • Spell mistakes, such as writing "printf" by "prontf"
  • Wrong typing of confusing symbols, such as l and 1, or O and 0.

The beginners who are not good at English frequently make these bugs.

  • Lack of declaration, such as use of an integer variable x without "int x;".
  • Drop "}" which close a block starting by a "{".

These are easy bugs because they give simple and understandable error messages.

  • Impossible process, such as access to x[10] after the declaration "int x[10];" (you can access only from x[0] to x[9]).
  • Unsuitable variable type, such as you use an integer for calculation of real value.
  • Wrong algorithm.
  • You do not get suitable libraries such as drop of "#include <stdlib.h>" when you use atof function.

These are more difficult bugs because you may get no errors when you compile it, and you find something wrong after you run it.

Skills of debugging

Read the error messages and understand!

Beginners get panic when they get an error and ask help of an experts immediately. Sit down. The situation may not be so serious. You should read the error message and make a guess to find the mistakes.

Use "man"

You can check how to use a function by using the man command:

$ man scanf 

Stop (exit) the program in the middle and see if the error happens before the exit or after the exit

You may sometimes get an error message "segmentation fault". It is a bad error. We cannot see the place of mistake from this message. To find the mistake, you should insert a function

exit(0);

to the program. It let the program stop and end at that place. If you do not get the error, it means the mistake exists after the exit(0); function. Then you move the exit(0); function to another place and try again. You repeat this process until you finally find where is the place of the malfunction.

Comment-out suspicious lines

You can temporarily remove some lines by comment-out (putting // at the head of the lines). If you get no error after comment-out of a line, it means the line is making a trouble.

Display and check values of variables

When you get no errors but the results are strange, something wrong in the internal process. You pick up some variable and insert "printf" function to display the value. You can check and consider whether it is reasonable or not. It gives hints of the solution.

Simplify the input and settings

Generally speaking, it is easier to find the mistakes in the simple situation. If the result looks bad, you should try the simplest case. If the result is still bad even in the simplest case, the error must be happening in a simple place.

Search for the error message in Google

If you see an unfamiliar error message, you may try searching for the message in Google. Then you may find somebody else who got the same error and who solved the problem.

Write a "good source code"

The source code is an intellectual property like an art. There is good / bad or beautiful / ugly. A good and beautiful code is simple and easy to read and understand. Possibility of errors are less than a bad and ugly source code. It is easy to find and fix the error for a good source code.

If your source code is bad and ugly, perhaps no one wants read it. It means you cannot get help!

How to write a good source code

  • Make it simple
  • Put useful comments
  • Make consistent rules in the writing style and keep them
  • Do version control
  • Do not depend on a default setting. Write explicitly.
  • Do not leave magic numbers in the middle of the code.
  • Learn from other people's good code.

The skill of writing a good code is mostly the same as skills of making good paper and good presentations. Consider deeply about what you mean and organize it in a simple order.

Frequent mistakes before bugs

The following simple and silly mistakes are not bugs. You cannot fix them by changing your source code:

  • After rewriting the source code, you compile it without saving the file.
  • You run a file which is not the result of your compile.
  • You output the result to a file in an append mode. The bad results always remain at the head of the output file.
  • You drop necessary options in the compile command, such as dropping "-lm" option when you use a mathematics library.

[Go back to C language introduction]

Last modified:2018/10/22 12:43:44
Keyword(s):
References:[C language introduction]