How to find and kill bugs for new college students

 The original text was published in the official account: Tao Ge is still ( scan the QR code, follow the official account, get the information for free )

image

 

On September 9th, 1947, the first bug in computer history was born on September 9th, 1947, when it was sunny. We introduced it in more detail in the brief history of computer development (link). For programmers, writing bugs, checking bugs, and killing bugs are all commonplace.

Many of my blog and public account followers are college students. People often send me a program to help me find the bug. If it is not a working day and is free, I usually reply: log debugging. Let’s take a look at just three examples:

 

College student 1:

image

 

College student 2:

image

 

College student 3:

image

 

Sometimes, the problem is really simple, and it can even be seen at a glance, but I will not point it out directly. It is better to teach people how to fish than to teach people how to fish. I hope he can debug it by himself and make progress. such as:

 

So, there is this article. Next time a college student friend asks you and me to help find bugs, give him this article. By the way, even though college students are talking about big guys, they still don’t dare to be. I have self-knowledge, and I'm far behind the real big guys.

Program debugging is an extremely important ability. Programmers, writing bugs, are basically not strange. In different scenarios, you need to use different methods to detect and kill bugs.

In actual work, it is often necessary to use eighteen martial arts, comprehensive use or try a variety of methods to effectively solve the problem. Objectively speaking, my own experience and methods of checking and killing bugs should be relatively rich. The most impressive thing is that a linux system bug was found. Later, after the linux source code was updated and a new version was released, the bug was resolved.

image

 

However, for most college students who are just learning programming, they only need to master a simple method to solve almost all problems. The method is: log debugging.

I take a simplified sample version of a piece of code I received before as an example (the original program is a bit more complicated):

#include <iostream>
using namespace std;

int main()
{
  int a = 1;
  int b = 2;
  int c = 3;
  int *p = 0;
  *p = 0;
  int d = a + b + c;

  cout << d << endl;
  return 0;
}

The question of the college student is: Why is there no output value of d, and want to know what is wrong?

 

In fact, if you run this program, there will be a message indicating which line is wrong! I now suppose that he doesn't know how to read the prompt message, how can he find out where the problem is? The answer is: log debugging. as follows:

#include <iostream>
using namespace std;

int main()
{
   cout << "xxx1" << endl;
   int a = 1;
   cout << "xxx2" << endl;
   int b = 2;
   cout << "xxx3" << endl;
   int c = 3;
   cout << "xxx4" << endl;
   int *p = 0;
   cout << "xxx5" << endl;
   *p = 0;
   cout << "xxx6" << endl;
   int d = a + b + c;
   cout << "xxx7" << endl;

   cout << d << endl;
   cout << "xxx8" << endl;
   return 0;
}

The output is:

xxx1

xxx2

xxx3

xxx4

xxx5

This is weird. There is xxx5 but not xxx6, so the error must be *p=0 here, so you can find the problem.

In short, for novices, logging debugging and viewing each step is the simplest and most feasible method. If this method can't solve it, let me see what the mutated bug is.

 

This article is very simple, not worth mentioning, I hope it will be useful to college students. It's Friday, I wish Merry Christmas and Happy weekend. Rest early!

Guess you like

Origin blog.csdn.net/stpeace/article/details/113065051
Recommended