当前位置:网站首页>【C】 Practical debugging skills
【C】 Practical debugging skills
2022-07-18 01:41:00 【Domeecky】
Catalog
View the current information of the program
Simulate the implementation of library functions :strcpy
debugging
Novice programmers 80% Time to write code ,20% Time to debug , Old programmer 20% Time to write code ,80% Time to debug .
What is debugging ?
The control code is executed step by step , The process of finding the cause of program errors according to various information is called debugging .
Why debugging is needed ?
When our code goes wrong , Looking only at the surface of the code is often unable to see the problem , We need to see things in the code through debugging .
Debug and Release
The code in our compiler can generate two versions :Debug and Release.

Before the code is executed , There are only three files in our file

After execution debug edition , More debug file

Then execute in sequence release edition , More release file

difference
debug For debug version , It contains debugging information , Without any optimization
release Release version for , Various optimizations have been carried out , Code size and performance are optimal



Shortcut key
F5
Start debugging , Often with F9 Cooperate to jump to the next breakpoint .

F9
Create breakpoints 、 Cancel breakpoint
The breakpoint : The control program stops , You can also create breakpoints by clicking on the left side of the mouse

Conditional breakpoints :
Stop when the program meets the set conditions , Often used in cycles .

example : Conditional expression , When i ==10 Stop at ,

When the next breakpoint is this breakpoint , Press down F5, Go straight to i ==10 Stop when .
F10
Deal with one by one , Think of a function as an entire instruction

F11
Inside the function

Ctrl+F5
Directly execute the program without debugging

notes : If there is Fn Key is required Fn+ The above shortcut keys take effect
Click here for more shortcut keys
View the current information of the program
View temporary variables


Look at the call stack
Clearly reflect the current function of the function call relationship level


View assembly information

Check register information

Debugging cases
What is the output of the following code ?
int main()
{
int i = 0;
int arr[10] = { 0 };
for (i = 0; i <= 12; i++)
{
arr[i] = 0;
printf("Hello");
}
return 0;
}answer : Dead cycle
Let's explore it through debugging


Through debugging, you can see that in i be in 0~11 It's all right ( When visiting 0, Set it to 0), When accessing the twelfth subscript , Come in and find i yes 12, When put arr [12] set 0 When ,i Also became 0, From this we can get the answer :arr [12] Namely i , When i Is set to 0, Enter the cycle again , This leads to an endless cycle .

Print out the addresses of the two variables , You can see that their addresses are the same .
Why do you have the same address ?
The way to store data in the stack is to use the high address first and then the low address . And the array grows with the subscript , The address changes from low to high .

When i and arr When there is a proper distance between , Using the array out of bounds operation, it is possible to cover i . The situation of each compiler is different .
How to write good code
1、 Use assert Avoid null pointers .
2、 Use as much as possible const .
3、 Add necessary comments .
Simulate the implementation of library functions :strcpy
strcpy Is a function that copies a string into another string variable . Need to include header file <string.h>.

void my_strcpy(char* arr1, char* arr2)
{
while (*arr2!='\0')
{
*arr1 = *arr2;
arr1++;
arr2++;
}
*arr1 = *arr2;// Will the last '\0' Copy it in, too
}First optimization :
void my_strcpy(char* arr1, char* arr2)
{
while (*arr2!='\0')
{
*arr1++ = *arr2++;// After assignment, directly ++
}
*arr1 = *arr2;// Will the last '\0' Copy it in, too
}Second optimization :
void my_strcpy(char* arr1, char* arr2)
{
while (*arr1++ = *arr2++)//(*arr1++ = *arr2++) Finally, return the assignment character ASCII Code value
{ // Finally, assign values first '\0', Then judge that the cycle condition is false , Exit loop
;// Execute an empty statement
}
}Third optimization :
void my_strcpy(char* arr1, const char* arr2)// Because the assigned string does not need to be changed , therefore const modification
{
while (*arr1++ = *arr2++)//(*arr1++ = *arr2++) Finally, return the assignment character ASCII Code value
{ // Finally, assign values first '\0', Then judge that the cycle condition is false , Exit loop
;// Execute an empty statement
}
}The fourth optimization :
void my_strcpy(char* arr1, const char* arr2)// Because the assigned string does not need to be changed , therefore const modification
{
assert(arr2 != NULL);// Assertion arr2 Not null pointer , If null pointer , Application error
assert(arr1 != NULL);
while (*arr1++ = *arr2++)//(*arr1++ = *arr2++) Finally, return the assignment character ASCII Code value
{ // Finally, assign values first '\0', Then judge that the cycle condition is false , Exit loop
;// Execute an empty statement
}
}边栏推荐
- STM32应用开发实践教程:环境参数持久化存储的应用开发
- Development practice - reasoning application development experience of shengteng cann
- 坚持写下去的原因
- 【C 练习】下列代码输出的结果是什么
- 网络安全(1)
- Is it safe to open an account online now? What is the reverse repurchase of treasury bonds?
- 手机浏览器产品分析
- 【C 练习】序列中删除指定的数字
- 现在网上开户安全么?想咨询一下,国债逆回购是什么?
- Staff information management system -- program design
猜你喜欢

C# 飞行棋小游戏

云小店商城源码修复30套模板支持一键对接各大系统

【C】 Const decorated pointer

云原生:Docker实践经验(三)Docker 上部署 MySQL8 主从复制

Introduction to redis

Matlab bottom source code realizes image corrosion and expansion operation (consistent with Halcon effect)

【C 练习】打印‘X’图形

【C】函数栈帧的创建和销毁

Realizing Halcon scale with MATLAB low-level source code_ image_ Max operator effect

Matlab bottom source code to realize image dynamic binarization
随机推荐
A Tong's little wish
Data Lake (19): SQL API reads Kafka data and writes it to iceberg table in real time
mysql面试
Thinking of reading
2020ccpc Qinhuangdao exam results (ruler)
Understanding at different stages
求字符串长度
【C】实用调试技巧
善良的人都晚熟
Redis transaction and Message Subscription Publishing
How Axure achieves screen adaptation
After writing a paper in 2 hours, why gpt-3 doesn't deserve a name?
基於機器學習的笑臉檢測
C. K-beautiful Strings
Node connects to the database for addition, deletion, modification and query
【C 练习】打印0~100000所有「水仙花数」
C# Winform窗体基础属性
数据传输:同构异IP数据源批量抽取实践
Summary of Halcon common image preprocessing operators
Codeforces Round #806 (Div. 4)