当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
八.数据的存储
Detailed explanation of new features of ES advanced function syntax
正式发布丨VS Code 1.70
DOM-DOM树,一个DOM树有三种类型的节点
flink The object probably contains or references non serializable fields.
Detailed explanation of new features of ES advanced array function syntax
Logstash日志数据写入异常排查问题总结
MSP430如何给板子下载程序?(IAR MSPFET CCS)
BUU brushing record
Salesforce解散中国团队,什么样的CRM产品更适合中国人
随机推荐
Add user error useradd: cannot open /etc/passwd
架构篇(二)架构的复杂度来源
Meaning of df and df -lh
Entity到Vo的转换
JS-DOM元素对象
正式发布丨VS Code 1.70
shell脚本入门
LitePal操作数据库
Detailed explanation of common methods of filtering matrix (array) elements in Matlab
alibaba数据同步组件canal的实践整理
Multi-threaded ThreadPoolExecutor
comp3331-9331-22t1-midterm复习辅导-tutorial week 5
聊聊对RPC的理解
Oops novice template Framework project guide
MySQL权限控制、分区表、快速复制表
ifconfig与ip命令的比较
Ninjutsu_v3_08_2020 - safety penetrating system installation
维特智能惯导配置
《如何戒掉坏习惯》读书笔记
(Nips-2015) Spatial Transformer Network