当前位置:网站首页>Practical debugging skills
Practical debugging skills
2022-07-02 15:11:00 【ふり】
Practical debugging skills
List of articles
- One 、 What is? bug?
- Two 、 What is debugging ? How important is it ?
- 3、 ... and 、Windows Introduction to environment debugging
- Four 、 Do more , Try debugging , To make progress .
- 5、 ... and 、 Some debugging examples
- 6、 ... and 、 How to write good ( Easy to debug ) Code for
- 7、 ... and 、 Common programming errors
One 、 What is? bug?
Program error , In English Bug,Bug The original meaning of the word is “ Bedbug ” or “ Worm ”. Now? , In a computer system or program , If there are some hidden undetected defects or problems , Also known as “Bug” that bug Why do you practice with computers ???
Early computers were very large , Some small insects may get inside the machine , Cause computer failure . The first in history “Bug” , It's really because a moth accidentally walked into a computer and caused a failure , therefore Bug Bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug bug .
The moth that was first found to cause a computer error , It was also the first computer program error .
Two 、 What is debugging ? How important is it ?
Everything that happened must have a trace to follow , If you have a clear conscience , There is no need to cover up and there is no sign , If you have a guilty conscience , It is necessary to cover up , Then there must be signs , The more signs, the easier it is to follow the vine , This is the way of reasoning .
Following this path down the river is a crime , Go against the current , It's the truth .
A good programmer is a good detective .
Every debugging is a process of trying to solve the case .
How we write code ?

How to check the problems ?
Refuse - Superstitious debugging !!!!
2.1 What is debugging ?
debugging ( English :Debugging / Debug), Also known as debugging , It is a process of finding and reducing program errors in computer programs or electronic instruments and equipment .
2.2 The basic steps of debugging
- Find the existence of program errors
- To isolate 、 Eliminate and so on
- Determine the cause of the error
- Propose solutions to correct errors
- Correct procedural errors , To test
2.3 Debug and Release Introduction to
- Debug Usually called debug version , It contains debugging information , And without any optimization , It's easy for programmers to debug programs .
- Release It's called a release , It's often optimized , Make the program in code size and running speed is optimal , So that users can use it well .
Debug Under the version :
Release Under the version :

You can see , The amount of memory occupied by different versions is different , This is a related optimization .
Debug and Release Disassembly shows comparison :
So we say debugging is Debug edition In the environment of , A process of finding potential problems in code .
What optimizations did the compiler do ?
// Changes of the same code in two different environments
#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;
}
debug Mode to compile , The result of the program is an endless loop .
release Mode to compile , There is no dead cycle in the program .
This is caused by optimization .
3、 ... and 、Windows Introduction to environment debugging
3.1 Preparation of debugging environment

Select... In the environment debug Options , To make the code debug normally .
3.2 Learn the shortcut keys

Common shortcut keys
F5
Start debugging , Often used to jump directly to the next breakpoint .
F9
Create breakpoints and cancel breakpoints The important role of breakpoints , Breakpoints can be set anywhere in the program .
In this way, the program can stop executing at the desired position , Then carry it out step by step .
F10
Process by process , Usually used to deal with a process , A procedure can be a function call , Or a statement .
F11
Sentence by sentence , Is to execute one statement at a time , But this shortcut can make our execution logic enter the function ( This is the most Long term ).
CTRL + F5
Start execution without debugging , If you want the program to run directly without debugging, you can use it directly .
Want to know more shortcuts ? Am I
3.3 Check the current information of the program during debugging
3.3.1 View the value of the temporary variable
After debugging starts , The value used to observe the variable .


3.3.2 View memory information
After debugging starts , Used to observe memory information .


3.3.3 Look at the call stack

Through the call stack , Can clearly respond to the call function and the location of the current call.
3.3.4 View assembly information
After debugging starts , There are two ways to go to assembly
( One )

( Two )
You can switch to assembly code .
3.3.5 Check register information

You can view the usage information of registers in the current running environment .
Four 、 Do more , Try debugging , To make progress .
- Be sure to master debugging skills .
- Beginners may 80% Time to write code ,20% Time to debug . But a programmer may 20% Time to write a program , however 80% Time to debug .
- We are talking about some simple debugging .
There may be very complex debugging scenarios in the future : Debugging of multithreaded programs, etc .- Use more shortcut keys , Improve efficiency .
5、 ... and 、 Some debugging examples
#include <stdio.h>
int main()
{
int i = 0;
int arr[10] = {
1,2,3,4,5,6,7,8,9,10 };
for (i = 0; i <= 12; i++)
{
arr[i] = 0;
printf("hehe\n");
}
return 0;
}
What is the result of the operation ?
Dead cycle , Why? ? At this time, if you don't debug, you don't know why .
Enter debugging , View the change of window variable value
When i=10 When , Has caused the array to be out of bounds , What will happen at this time ?

The array is out of bounds
When arr[12] = 0 When , You'll find that i It turned into 0 了 , Why is that ? We put i and arr[12] Take out your address and have a look :
i and arr[12] Your address is actually the same

6、 ... and 、 How to write good ( Easy to debug ) Code for
6.1 Good code :
- The code is working properly
- bug Very few
- Efficient
- High readability
- High maintainability
- The notes are clear
- The documentation is complete
common coding skill :
- Use assert
- Use as much as possible const
- Develop a good coding style
- Add the necessary comments
- Avoid the pitfalls of coding .
6.2 demonstration :
Simulate the implementation of library functions :strcpy
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest,const char* src)
{
char* ret = dest;
// Assertion
assert(dest != NULL);
assert(src != NULL);
/*while (*dest!='\0') { *dest = *src; *dest++; *src++; } *dest = *src;*/
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "XXXXXXXXX";
char arr2[] = "Hello world";
// Copy at the same time '\0'
my_strcpy(arr1, arr2);
printf("%s\n", my_strcpy(arr1, arr2));
return 0;
}
assert Assertion : Avoid copying null pointers
const The role of :
- const If you put it in * Left side , It modifies what the pointer points to , Ensure that the content pointed to by the pointer cannot be changed by the pointer
change . But the contents of the pointer variable itself are variable .- const If you put it in * To the right of , The modification is the pointer variable itself , It ensures that the contents of pointer variables cannot be modified , But it means
What the needle points to , It can be changed by the pointer .

practice :
The simulation implements a strlen function :
#include <stdio.h>
#include <assert.h>
int my_strlen(const char* str)
{
assert(str != NULL);
int count = 0;
while (*str)
{
count++;
str++;
}
return count;
}
int main()
{
const char arr1[] = "absdef";
int len = my_strlen(arr1);
printf("len = %d\n", len);
return 0;
}
7、 ... and 、 Common programming errors
7.1 Compiler error
Look directly at the error message ( double-click ), solve the problem . Or you can do it with experience . Relatively simple .
7.2 Linked error
Look at the error message , Mainly find the identifier in the error message in the code , Then locate the problem . Generally, the identifier name is not There are or spelling mistakes .
7.3 Runtime error
With the help of debugging , Gradually locate the problem . It's the hardest thing to do .
reminder :
Be a conscientious person , Accumulate troubleshooting experience .
边栏推荐
- PHP method to get the index value of the array item with the largest key value in the array
- Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
- Socket and socket address
- C#代码审计实战+前置知识
- Tidb data migration tool overview
- Deploy tidb cluster with tiup
- Record an error report, solve the experience, rely on repetition
- Sharp tool SPL for post SQL calculation
- info [email protected]: The platform “win32“ is incompatible with this module.
- 如何对 TiDB 进行 TPC-C 测试
猜你喜欢

GeoServer offline map service construction and layer Publishing

Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)

c语言入门--数组

Leetcode - Search 2D matrix

Internet Explorer officially retired

Large top heap, small top heap and heap sequencing

Dragonfly low code security tool platform development path

Introduction to mathjax (web display of mathematical formulas, vector)

Base64 编码原来还可以这么理解

TiDB数据迁移工具概览
随机推荐
广州市应急管理局发布7月高温高湿化工安全提醒
Base64 coding can be understood this way
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
C语言习题---(数组)
TiDB数据迁移场景综述
Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
Obsidian installs third-party plug-ins - unable to load plug-ins
Wechat applet uses towxml to display formula
C thread transfer parameters
Tidb data migration tool overview
[noi simulation] Elis (greedy, simulation)
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
TiDB混合部署拓扑
学习使用php实现公历农历转换的方法代码
LeetCode 2310. The number of digits is the sum of integers of K
Fundamentals of software testing
Have you learned the wrong usage of foreach
记一次面试
数据分析常见的英文缩写(一)
kibana 基础操作