Practical Debugging Tips (2)

Foreword:

Here debugging skills (1) we introduced what is debugging.
We will list some examples to let us know more about and use debugging proficiently.

1. Debugging example

1.1 Example 1

Error code demonstration:
insert image description here
If we input 3, we expect to output 9, but the actual output is 15.
Here we have to find our problem.
1) First speculate on the cause of the problem, and preliminarily determine the possible cause of the problem.
2) Actually manual debugging is very important.
3) When debugging, we must know what to do.
insert image description here

When i=1, ret=1, sum=1, display normal
insert image description here
i=2, ret=2, sum=3, display normal
insert image description here
i=3, ret=c, sum=f (here we can see The result to sum is different from our expected result, which is caused by ret, and there is a logical problem)
Correction:
insert image description here
The program is written.

1.2 Example 2

#include <stdio.h>
int main()
{
    
    
    int i = 0;
    int arr[10] = {
    
     0 };
    for (i = 0; i <= 12; i++)
    {
    
    
        arr[i] = 0;//数组越界
        printf("hehe\n");
    }
    return 0;
}

The reason for the infinite loop (this is a coincidence, different compilers have different coincidences)
is the problem of array crossing.
As the subscript increases, the address of the array changes from low to high.
i and arr are local variables, which are placed on the stack area, because the memory usage habit of the stack area: first use the high address space, and then use the low address space. After the array is out of bounds, it happens that arr[12] has the same address as i, arr[12]=0, making i=0, which leads to an infinite loop of the program.
correct:
insert image description here

2. How to write good (easy to debug) code

2.1 What is good code

1) The code runs normally
2) Few bugs (does not affect normal use)
3) High efficiency (we can execute the results within the expected time frame)
4) High readability (the code is easier to identify)
5) High maintainability (Convenient for others to maintain)
6) Clear comments (make it easier for others to understand)
7) Complete documentation
Common coding skills:
1) Use assert
2) Try to use const
3) Develop a good coding style
4) Add necessary comments
5) Avoid coding pitfalls

2.2 Introduction to assert and const

2.2.1 Introduction to the assert function
To use the assert function, the header file <assert.h> must be included
Case:
insert image description here
Comparison
insert image description here

Analysis:
assert means to assert. If the judgment result is true, execute it directly; if the judgment result is false, report an error, determine the error of the pointer variable, and print the number of wrong lines on the screen.
Note:
1) Analyze the design of parameters (name, type), design of return value type
2) The harm of wild pointers and null pointers
3) The role of sasert
4) The role of const in the parameter part If
the target address and source address are reversed, it will cause Unable to execute,
2.2.2 Introduction to the const keyword
Case:

#include <stdio.h>
//代码1
void test1(void)
{
    
    
    int i = 10;
    int j = 20;
    int* p = &i;
    *p = 30;
    p = &j;
}
//代码2
void test3(void)
{
    
    
    int i = 10;
    int j = 20;
    const int* p = &i;
    *p = 30;//报错
    p = &j;
}
//代码3
void test3(void)
{
    
    
    int i = 10;
    int j = 20;
    int*const p = &i;
    *p = 30;
    p = &j;//报错
}
int main()
{
    
    
    //测试无const
    test1();
    //测试const在*的左边
    test2();
    //测试const在*的右边
    test3();
    return 0;
}

insert image description here
insert image description here
insert image description here
Conclusion:
When const modifies a pointer variable:
1) When const is placed on the left of *, the content pointed to by the pointer variable is limited, so the value of the memory pointed to by the pointer cannot be changed through the pointer variable, but the pointer variable itself can be changed.
2) When const is placed on the right side of *, it modifies the pointer variable itself, and the content of the pointer variable cannot be changed, but the content pointed to by the pointer variable can be changed through the pointer.

3. Common mistakes in programming

3.1 Compilation errors

Compilation errors (syntax problems, errors generated during compilation): directly read the error message prompt, and then double-click to solve the problem. Or it can be done with experience, which is relatively simple.

3.2 Linked errors

Linking errors (errors that occur during linking, unresolved external symbols): Look at the error message, mainly find the identifier in the error message on the computer, and then locate the problem. Usually the identifier name does not exist or is misspelled .

3.3 Runtime errors

Runtime errors (there are no grammatical and linking problems, but there are logical problems): You can only use debugging to gradually locate the problem and then correct it, which is the most difficult thing to do.

Guess you like

Origin blog.csdn.net/plj521/article/details/131484292