当前位置:网站首页>Summary of debugging skills
Summary of debugging skills
2022-08-11 02:55:00 【I'm going ashore~】
目录
4.1 实现代码:求 1!+2!+3! ...+ n! ;不考虑溢出
1.调试是什么?
调试(英语:Debugging / Debug),又称除错,是发现和减少计算机程序或电子仪器设备中程序
错误的一个过程
2. 调试的基本步骤
发现程序错误的存在
以隔离、消除等方式对错误进行定位
确定错误产生的原因
提出纠正错误的解决办法
对程序错误予以改正,重新测试
3.Debug和Release的介绍
Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.
Release 称为发布版本,它往往是进行了各种优化,使得程序在代码大小和运行速度上都是最优
的,以便用户很好地使用.
4. 调试实例
4.1 实现代码:求 1!+2!+3! ...+ n! ;不考虑溢出
#include <stdio.h>
int main()
{
int n = 0;
scanf("%d", &n);
int ret = 1;
int sum = 0;
for (n = 1; n <= 3; n++)
{
int i = 0;
for (i = 1; i <= n; i++)
{
ret *= i;
}
sum += ret;
}
printf("%d\n", sum);
return 0;
}
1!+2!+3!= 9 But the above code to calculate the result is15.This shows that we have a problem with my code,开始进行调试.

当我们进行计算3的阶乘的时候,ret的值还是2,This kind of code written from1Has been take to the desires of the number of,So we should treat each into the circulation ofret的值设为1.
正确代码
int main()
{
int n = 0;
scanf("%d", &n);
int ret = 1;
int i = 0;
int sum = 0;
for (i = 1; i <= n; i++)
{
ret *= i;
sum += ret;
}
printf("%d", sum);
return 0;
}4.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;
}首先,We can easily see an array access,But in the code, the results of the results in different environment is not the same.在vs中,It will be infinite loop.

解释:Because memory stack area using habit is to use high address space,再使用低地址空间,So the operating system is in the stack is assigned to a local variable firsti一块空间,Then assign an array a piece of space,The memory array is continuous deposit,It as the growth of the subscript address from low to high.And we are putting the elements in the array0之后,在vsAn array of,vsAfter the compiler will give an array of stored on digital0,但是vsVariables and arrays in memory location differ in two bytes,当程序到arr[12]的时候,In fact is access to the variablei的空间,此时将arr[12]Placing data0,In fact is the variable i 中的值修改为了0,Because both point to the same piece of memory space at this time.
再次注意,Different compiler environment,The results may be different.在vc6.0中 i 和 arr There is no space in memory.
而在gcc中,Difference between a byte,在vsThe difference between the two bytes of the.
5.如何写出好(易于调试)的代码.
5.1 优秀的代码:
1. 代码运行正常
2. bug很少
3. 效率高
4. 可读性高
5. 可维护性高
6. 注释清晰
7. 文档齐全
常见的coding技巧:
1. 使用assert
2. 尽量使用const
3. 养成良好的编码风格
4. 添加必要的注释
5. 避免编码的陷阱.
5.2 示范
模拟实现strcpy,Bloggers display continuously optimize the code
Bloggers display continuously optimize the code,满分为10分
方法一,5分
void my_strcpy(char* dest, char* src)//Incoming two Pointers
{
while (*src != '\0')
{
*dest = *src;
src++;
dest++;
}
*dest = *src; //这里最后将src指向的'\0'赋给dest指向的区域
}
int main()
{
char arr1[] = "hello haha";
char arr2[20] = "xxxxxxxxxxxxxx";
my_strcpy(arr2,arr1);
printf("%s\n", arr2);
return 0;
}方法二,6分
void my_strcpy(char* dest, char* src)//Incoming two Pointers
{
while (*src != '\0')
{
*dest++ = *src++;
}
*dest = *src; //这里最后将src指向的'\0'赋给dest指向的区域
}
int main()
{
char arr1[] = "hello haha";
char arr2[20] = "xxxxxxxxxxxxxx";
my_strcpy(arr2,arr1);
printf("%s\n", arr2);
return 0;
}方法三,7分
void my_strcpy(char* dest, char* src)//Incoming two Pointers
{
while (*dest++ = *src++)
{
;
}
}
int main()
{
char arr1[] = "hello haha";
char arr2[20] = "xxxxxxxxxxxxxx";
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
解释一下,这句语句,其实就是先将*src赋给*dest,然后src++,dest++,当src指向‘\0’的时候,先将'\0'赋给dest,但是此时*dest++ = *src++ 这个表达式的值为0,循环就结束了.
方法四,8分
#include <stdio.h>
#include <assert.h>
void my_strcpy(char* dest, char* src)
{
assret(src != NULL);
assret(dest != NULL);
while (*dest++ = *src++)
{
;
}
}
int main()
{
char arr1[] = "hello haha";
char arr2[20] = "xxxxxxxxxxxxxx";
int* p = NULL;
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}这里,用到了一个函数assert(断言),The reason for this is that we in the participation in,Sometimes mistakenly would transmit the null pointer to go in,This time will appear abnormal, speaking, reading and writing,而使用assert之后呢,Can quickly to help us locate the problem,Because when you pass a null pointer,Don't even let you compile the past.
方法5, 9分
void my_strcpy(char* dest, char* src)
{
assret(src && dest);
while (*dest++ = *src++)
{
;
}
}
int main()
{
char arr1[] = "hello haha";
char arr2[20] = "xxxxxxxxxxxxxx";
//int* p = NULL;
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}Here is the twoassert组合了起来.
方法6,满分

不知道你有没有注意到strcpyReturns a character pointer,还有src前面有一个const修饰.We want to simulate will be the best. my_strcpy函数设计返回值类型是为了实现函数的链式访问.
#include <stdio.h>
#include <assert.h>
char* my_strcpy(char* dest, const char* src)
{
assert(src && dest);
char* ret = dest;
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[] = "hello haha";
char arr2[20] = "xxxxxxxxxxxxxx";
//int* p = NULL;
printf("%s\n", my_strcpy(arr2, arr1));
return 0;
}6.补充知识
6.1 const讲解

int main()
{
//const int num = 10; //Compile different here,Because the expression must be can modify the value of the
//num = 20; //这里的numAt this point is often variable,所以不可被修改.
int num = 20;
int n = 0;
const int* p1 = #
p1 = &n;
int* const p2 = #
*p2 = 10;
return 0;
}const 可以修饰指针
const 放在*的左边(const int* p1;)
const修饰的是*p1,表示p1指向的对象不能通过p1来改变,但是p1变量中的地址是可以改变的
const 放在*的右边(int* const p2;)
const 修饰的是p2,表示p2的内容不能被改变,但是p2指向的的对象是可以通过p来改变的
7.编译常见的错误
编译型错误就是语法错误
链接型错误
运行时错误 - 借助调试解决的错误
#include <stdio.h>
int Add(int x, int y)
{
return x + y;
}
int main()
{
int a = 10;
int b = 20;
int c = Add(a, b);
printf("%d\n", c);
return 0;
}
void* p;
void test(void)
{
printf("hehe\n");
}
int main()
{
return 0;
}边栏推荐
- 四大组件---ContentResolver
- flink The object probably contains or references non serializable fields.
- [oops-framework] Template project [oops-game-kit] Introduction
- Mysq_Note4
- Window function application of sum and count
- 思念家乡的月亮
- postgresql ilike create function
- 广州纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
- 自动生成数据库设计文档利器
- 117. 本地开发好的 SAP UI5 应用部署到 ABAP 服务器时,中文字符变成乱码的原因分析和解决方案
猜你喜欢

How to solve the problem of Tomcat booting and crashing

Official release丨VS Code 1.70

Summary of Logstash log data write exception troubleshooting

CSAPP Data Lab

【Unity入门计划】Unity2D动画(1)-动画系统的组成及功能的使用

正式发布丨VS Code 1.70
OpenHarmony啃论文俱乐部-啃论文心得

BUU brushing record

OpenCV创始人:开源绝不能完全免费!

Inter-process communication method (2) Named pipe
随机推荐
WeChat public account background management
sql 使用到where和groupby时建立索引结果为啥是这样,原理是什么?
postgresql ilike create function
Oops novice template Framework project guide
Detailed explanation of new features of ES advanced function syntax
ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)
Research on the Application of Privacy Computing Fusion
今天聊聊接口幂等性校验
flink The object probably contains or references non serializable fields.
leetcode: 358. Reorder strings at K distance intervals
OpenCV founder: Open source must not be completely free!
DOM树的遍历-----修改样式,选择元素,创建和删除节点
ROS源代码阅读(1)
求和、计数的窗口函数应用
Mysq_Note4
基于FPGA状态机的自动售货机功能实现
调试技巧总结
学军中学推理社2017届招新试题
Js prototype and prototype chain and prototype inheritance
SIT221 Data Structures and Algorithms课程辅导