当前位置:网站首页>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 .
边栏推荐
- LeetCode 209. Minimum length subarray
- 关于网页中的文本选择以及统计选中文本长度
- Application and practice of Jenkins pipeline
- Topology architecture of the minimum deployment of tidb cluster
- Base64 coding can be understood this way
- Solve the problem that El radio group cannot be edited after echo
- 传感器数据怎么写入电脑数据库
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- 【NOI模拟赛】刮痧(动态规划)
- C#延时、在线程中开启定时器、获取系统时间
猜你喜欢
Kityformula editor configure font size and spacing
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
Internet Explorer officially retired
geoserver离线地图服务搭建和图层发布
Why can't programmers who can only program become excellent developers?
05_队列
The past and present lives of visual page building tools
TiDB数据迁移工具概览
关于网页中的文本选择以及统计选中文本长度
Wechat applet uses towxml to display formula
随机推荐
Tidb environment and system configuration check
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
Kityformula editor configure font size and spacing
记一次报错解决经历依赖重复
C code audit practice + pre knowledge
kityformula-editor 配置字号和间距
Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
kibana 基础操作
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
【C语音】详解指针进阶和注意点(2)
C语言中的算术运算及相关练习题
CTO如何帮助业务?
Find the maximum inscribed circle of the contour
Recommended configuration of tidb software and hardware environment
Record an interview
How to test tidb with sysbench
MFC 定时器使用
Sharp tool SPL for post SQL calculation
可视化搭建页面工具的前世今生