Program debugging common problems and solutions

1, CE (Compile Error) compilation error

In fact, many times by the error message is returned will be able to find the error.

① missing symbols, as shown below

Missing semicolon
Error expressed in the first sentence "printf" before missing a semicolon,
the second sentence Error indicates "return" before missing a semicolon.
Usually in the wrong line on the line to find the missing semicolon. such as,

    int main()
    {
        int sas   //here;
        printf("%d",a[7]);
        if(1)
        a[0]=1   //here;
        return 0;
    }
    
    

According to the code information and the picture above, it is easy to determine where the error.

② missing parameters

Like this:

void f(int a)
{
    ......
}
int main()
{
    f();
    return 0;
}
    
    

This error message will be returned:

[Error] too few arguments to function ‘void f(int)’

We give instructions in the use of function parameters is too small.

③ If the STL, STL and is related statements CE, then an error message can be complex.

But do not be afraid, how complicated errors can not.

So says a common problem following sort.

#include<cstdio>
#include<algorithm>
using namespace std;
struct A
{
    int v,c;
}a[15];
int main()
{
    for(int i=1;i<=10;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+11);
}
    
    

CE ??? and then the error message spicy so many QAQ
also pop up a bunch of code:

  template<typename _RandomAccessIterator>
    void
    __insertion_sort(_RandomAccessIterator __first,
             _RandomAccessIterator __last)
    {
      if (__first == __last)
    return;

      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
    {
      if (*__i < *__first)
        {
          typename iterator_traits<_RandomAccessIterator>::value_type
        __val = _GLIBCXX_MOVE(*__i);
          _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
          *__first = _GLIBCXX_MOVE(__val);
        }
      else
        std::__unguarded_linear_insert(__i);
    }
   }
    
    

Read ah QAQ
but that's OK, we know this is the wrong line:

if (*__i < *__first){···}

What does it mean? Think sort is used to do? Sort, right. Our A is a structure, which contains c, v two numbers.
So to conclude: the program does not know in what sort of.
How to do it? Cmp handwriting function.
If you want to sort by c, so:

bool cmp(A a,A b)
{
    return a.c<b.c;
}
    
    

Then sort change it:

sort(a+1,a+11,cmp);

In this way, compile!

2, RE (Running Error) run-time error

Run-time crash

FIG little big = V =

Specifically, there are the following solutions:
① check in scanf whether to join the% & other necessary symbols.
② check whether the array bounds (array index is negative, the array subscript is too large).
③ If the STL, please carefully check the statements related to the STL, whether it is a function or data structure.
④ If the pointer, check whether the pointer is initialized to NULL.
⑤ Check whether recursion too many times, causing the explosion stack.

3、WA(Wrong Answer)

Think about the correctness of their own algorithm it.

Algorithm right? look down:

First, the program output of the unexpected results, such as 0, maximum value.

① no initial value is not an array?
② is not illegal access memory (sometimes illegal memory access will not RE)?
③ whether the number of cycles is far above or below expectations?
④ is not int the explosion?

Second, the results are not consistent with the projections of the program's output, but not very different.

① initial value assigned to see if there is wrong.
② see if there is the "==" instead of "=."
③ see if there confuse ">" "<" "> =" "<="
④ variable type, right? Right output type (e.g. scanf ( "% c", ( int) a);)?

Third, the skilled use of debugging function.

Do not have to turn on debug panel debug output intermediate variables is also a good way.
One (or more) key variables change in the gap between the program and the expected changes can often find the problem by comparison.

4、AC(Accepted)

Then you are Bang Bang yo! ⊙v⊙

Well, the future will be updated.

Published 75 original articles · won praise 80 · views 20000 +

1、CE(Compile Error)编译错误

其实很多时候通过返回的错误信息就能找出错误。

①缺失符号,如下图

Missing semicolon
第一句Error表示在“printf”前少了一个分号,
第二句Error表示在“return”前少了一个分号。
通常在错误行的上一行找缺失的分号。比如,

    int main()
    {
        int sas   //here;
        printf("%d",a[7]);
        if(1)
        a[0]=1   //here;
        return 0;
    }
  
  

根据代码和上面图片的信息,很容易判断错误在哪。

②缺失参数

比如这样:

void f(int a)
{
    ......
}
int main()
{
    f();
    return 0;
}
  
  

就会有这样的错误信息返回:

[Error] too few arguments to function ‘void f(int)’

说明我们在使用函数时给予的参数过少。

③如果使用了STL,并且是STL相关语句CE,那么错误信息可能很复杂。

但是不要怕,错误可能并不是多么复杂。

所以下面说一个sort常见的问题。

#include<cstdio>
#include<algorithm>
using namespace std;
struct A
{
    int v,c;
}a[15];
int main()
{
    for(int i=1;i<=10;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+11);
}
  
  

然后就CE 了··· 而且错误信息辣么多QAQ
还会跳出来一串代码:

  template<typename _RandomAccessIterator>
    void
    __insertion_sort(_RandomAccessIterator __first,
             _RandomAccessIterator __last)
    {
      if (__first == __last)
    return;

      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
    {
      if (*__i < *__first)
        {
          typename iterator_traits<_RandomAccessIterator>::value_type
        __val = _GLIBCXX_MOVE(*__i);
          _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
          *__first = _GLIBCXX_MOVE(__val);
        }
      else
        std::__unguarded_linear_insert(__i);
    }
   }
  
  

看不懂啊QAQ
不过没关系,我们知道错误行是这个:

if (*__i < *__first){···}

啥意思呢?想想sort是用来干什么的?排序,对吧。而我们的a是一个结构体,里面包含着c,v两个数。
于是就得出结论:程序不知道按什么来排序了。
怎么办呢?手写cmp函数。
如果想按c排序,就这样:

bool cmp(A a,A b)
{
    return a.c<b.c;
}
  
  

然后把sort改一下:

sort(a+1,a+11,cmp);

就这样,编译通过!

2、RE(Running Error)运行时错误

Run-time crash

图有点大=V=

Specifically, there are the following solutions:
① check in scanf whether to join the% & other necessary symbols.
② check whether the array bounds (array index is negative, the array subscript is too large).
③ If the STL, please carefully check the statements related to the STL, whether it is a function or data structure.
④ If the pointer, check whether the pointer is initialized to NULL.
⑤ Check whether recursion too many times, causing the explosion stack.

3、WA(Wrong Answer)

Think about the correctness of their own algorithm it.

Algorithm right? look down:

First, the program output of the unexpected results, such as 0, maximum value.

① no initial value is not an array?
② is not illegal access memory (sometimes illegal memory access will not RE)?
③ whether the number of cycles is far above or below expectations?
④ is not int the explosion?

Second, the results are not consistent with the projections of the program's output, but not very different.

① initial value assigned to see if there is wrong.
② see if there is the "==" instead of "=."
③ see if there confuse ">" "<" "> =" "<="
④ variable type, right? Right output type (e.g. scanf ( "% c", ( int) a);)?

Third, the skilled use of debugging function.

Do not have to turn on debug panel debug output intermediate variables is also a good way.
One (or more) key variables change in the gap between the program and the expected changes can often find the problem by comparison.

4、AC(Accepted)

Then you are Bang Bang yo! ⊙v⊙

Well, the future will be updated.

Guess you like

Origin blog.csdn.net/qq_36693514/article/details/77905589