当前位置:网站首页>Visual studio 2022 debugging skills introduction

Visual studio 2022 debugging skills introduction

2022-08-09 22:07:00 playful green shaft

保持空杯心态,为自己打气!

1.什么是bug?

bug是计算机领域专业术语,bug原意是“臭虫”,现在用来指代计算机上存在的漏洞,原因是系统安全策略上存在的缺陷,有攻击者能够在未授权的情况下访问的危害.
漏洞是在硬件、软件、协议的具体实现或系统安全策略上存在的缺陷,从而可以使攻击者能够在未授权的情况下访问或破坏系统.

2.调试是什么,如何调试?

2.1 调试的概念

Debugging simply means that debugging is a necessary procedure to ensure the normal operation of the provided equipment.
Check the code with some tools,修复bug.

2.2调试基本步骤

1.Confirm that the program is wrong
2.Use various methods to locate errors
3.Use the tool to confirm the cause of the error
4.Come up with solutions to errors
5.Make changes to the program,重新测试

3.release和debug介绍

Debug通常是调试版本,它包含调试信息,并且不做任何优化,便于程序员调试(This version is dedicated to programmers)
release称为发布版本,It tends to be optimized for code speed and code size,以便于用户使用.

Debug版本和release版本比较:

显然,releaseThe space size of the version is larger than that of debugThe space size is smaller.

还有一点就是:releaseThe version is not debugged,debugThe version has debug information,因为releaseVersions are for users,And users do not know programming and debugging,So there is no debug information.

4.visual studio 2022Editor shortcut key learning

4.1 关键一步

了解到debugVersions are debuggable,所以在使用visual studio 2022The first environment is when the version is configureddebug版本:

4.2 快捷键介绍

4.2.1 常用快捷键

4.2.1.1 生成快捷键

Ctrl+Shift+B:生成解决方案

Ctrl+F7:编译

Alt+F11:Run code analysis on the solution

4.2.1.2 Debugging is commonly used and fast

Ctrl+Shift+F9:删除所有断点

Ctrl+Alt+E:异常

Ctrl+Alt+Q:快速监视(或者Shift+F9)

Ctrl+Shift+F5:重启(Use after debugging)

Ctrl+F10:运行到光标处

F5:启动调试,Often used in conjunction with breakpoints.先是F9Find the location you want to detect,然后F5进行调试,You can check directly from the breakpoint.

Ctrl+F5:Run the result directly without debugging.

F11:逐语句,A more detailed observation,You can jump directly into the function internal detection.

Shift+F11:单步跳出(跳出调试)

F10:逐过程,Can handle a process(函数、语句)

F9:创建断点和取消断点
The purpose of breakpoints is to allow the program to jump to the desired location and execute at will,继而一步步执行下去.

Alt+6:启动内存1(需要先F10It can only be used after debugging,启动内存2:Ctrl+Alt+2;启动内存3:Ctrl+Alt+3)

Ctrl+Alt+V+A 自动窗口(It can only be used after debugging)

Ctrl+Alt+C:调用堆栈(It can only be used after debugging,或者用Alt+7)

Alt+8:转到反汇编

Ctrl+Alt+W+1:Start the watch window1(Start the watch window2:Ctrl+Alt+W+2;Start the watch window3;Ctrl+Alt+W+3;Start the watch window4:Ctrl+Alt+W+4)

Alt+4:局部变量窗口

Alt+5:寄存器

Shift+F5:停止调试

4.2.1.3 Edit frequently used shortcut keys

Alt+向右键:完成单词(Ctrl+空格键)

Ctrl+F:查找

Ctrl+Shift+F:在文件中查找

Ctrl+G:转到

Ctrl+F12:转到声明

F12:转到定义(The premise is that there must be a statement)

Ctrl+,:导航到

Alt+F12:查看定义

Ctrl+H:替换

Ctrl+Z:撤销

Ctrl+K+C:注释所选内容

Ctrl+K+U:Cancel the selected comment content

4.2.1.4 Common shortcut keys for files

Alt+F4:退出文件

Ctrl+N:新建文件

Ctrl+Shift+N:新建项目

Ctrl+O:打开文件

Ctrl+Shift+O:打开项目

F2:重命名(The mouse must be over the filename to rename)

Ctrl+Shift+S:全部保存

4.2.1.5 Common shortcuts for projects

Shift+Alt+A:添加现有项

Ctrl+Shift+A:添加新项

The above shortcuts are based on my defaultvisual studio 2022Selected frequently used and practical shortcut keys,If it is different on your compiler, it can be taken according to the shortcut message provided by the compiler,See also this link:visual studio 2022键盘快捷方式

4.2.2 There is information that needs to be viewed when debugging

4.2.2.1 查看临时变量

4.2.2.2 View memory messages

4.2.2.3 查看调用堆栈

4.2.2.4 查看汇编信息

4.2.2.5 查看寄存器信息

5.调试实例

5.1 实例一

求 1!+2!+3! …+ n! ;不考虑溢出.

int main()
{
    
	int i = 0;
	int sum = 0;//保存最终结果
	int n = 0;
	int ret = 1;//保存n的阶乘
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
    
		int j = 0;
		for (j = 1; j <= i; j++)
		{
    
			ret *= j;
		}
		sum += ret;
	}
	printf("%d\n", sum);
	return 0;
}

这段代码输出;

为什么1!+2!+3!的阶乘是15呢?不应该是9吗?,下面F10Debug it and take a look:

The final resolved code:

int main()
{
    
	int i = 0;
	int sum = 0;//保存最终结果
	int n = 0;
	int ret = 1;//保存n的阶乘
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
    
		int j = 0;
		ret = 1;
		for (j = 1; j <= i; j++)
		{
    
			ret *= j;
		}
		sum += ret;
	}
	printf("%d\n", sum);
	return 0;
}

5.2 实例二

Ask this code indebug版本下x86平台下,为什么是死循环?

#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;
}

Now let's debug to explain why?

真有这么巧吗?
其实并不是在VC6.0中arr[9]和iThere is actually no extra space between variables,在gcc编译器下arr[9]和iThere is an integer space in the middle of the variable,visual studio 2022\2019\2013中的x86平台下arr[9]和iThe space between the variables consists of two integers.这些都不是巧合,Rather, it is specified by the editor itself,Different compilers have different rules.

6.How to write good code

6.1 优秀的代码

1.代码运行正常
2.bug很少
3.效率高
4.可维护性高
5.可读性高
6.注释清晰
7.文档齐全

6.2 Demonstration with examples

模拟实现库函数:strcpy

The first is to create two arrays,An array is the destination array and an array is the source array,strcpyThe library function is to copy the source array to the target array,Then we can create pointers with pointers,Point to the starting position of the two arrays respectively,Then use the pointer offset to place the elements of the source array in the destination array,最后’\0’Also placed in the destination array,So as to achieve the realization of library functionsstrcpy(basic idea)

6.2.1 方法一:Realize according to the idea

void my_strcpy(char* dest, char* src)
{
    
	while (*src != '\0')
	{
    
		*dest++ = *src++;
	}
	*dest = *src;
}


int main()
{
    
	char arr1[20] = "xxxxxxxxxx";
	//xxxxxxxxxx
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

6.2.2 进一步优化

void my_strcpy(char* dest, char* src)
{
    
	//停止条件就是'\0','\0'对应的ASCII码值是0
	while (*dest++ = *src++)
	{
    
		;
	}
}
int main()
{
    
	char arr1[20] = "xxxxxxxxxx";
	//xxxxxxxxxx
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

6.2.3 用断言assertLibrary functions are further optimized

对assert函数介绍:

简单来说,assertThe library function is to ensure the validity of the pointer.

void my_strcpy(char* dest, char* src)
{
    
	assert(dest != NULL);//断言
	assert(src != NULL);//断言
	//NULL是#define NULL ((void *)0)

	while (*dest++ = *src++)
	{
    
		;
	}
}
int main()
{
    
	char arr1[20] = "xxxxxxxxxx";
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

6.2.4 Further optimization

void my_strcpy(char* dest, char* src)
{
    
    assert(dest&&src);
    //NULL是#define NULL ((void *)0)

	while (*dest++ = *src++)
	{
    
		;
	}
}
int main()
{
    
	char arr1[20] = "xxxxxxxxxx";
	char arr2[] = "hello";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

6.2.5 The final step is to refine and optimize

//strcpy有返回类型,The source array is immutable for these requirements

char* my_strcpy(char* dest, const char* src)
{
    
	char* ret = dest;
	assert(dest && src);//断言

	while (*dest++ = *src++)
	{
    
		;
	}
	return ret;
}
int main()
{
    
	char arr1[20] = "xxxxxxxxxx";
	char arr2[] = "hello";

	printf("%s\n", my_strcpy(arr1, arr2));

	return 0;
}

This is the process of slowly optimizing the details after debugging.过程比结果重要,So focus on the details of the process.

完善优化中constKeyword understood again

首先我们要知道const的作用:

告知程序员这里不能修改,具有“自描述”意思
Inform the editor that this cannot be modified,An error will be reported if repaired

1.constThe keyword modifier variable context


这里的const修饰的是变量,So this variable cannot be modified.But it can be modified indirectly with a pointer,如:

为什么呢?
可以这样理解,因为constJust decorated the variablea,所以不能直接用a来修改,But can be founda的地址进行修改,不是直接修改a,but indirect modification,下面看constWhen the keyword repair is a pointer, the thinking will be clearer.

2.constSee keyword-modified array operands

这里的constModified variables have constant properties,But this constant means that it cannot be modified,Instead of actually turning the variable into a constant,这是不可能的.

3.constKeywords decorate pointer scenarios

Let's talk about pointers,int * p,这里的pA pointer variable is used to store an address,*is the pointer sign,int是指针的类型(Determines how many bytes are accessed when the pointer is accessed and the step size of the pointer)

3.1 const int *p

3.2 int const *p

这里int const *p和上述const int p是一样的,constAll are retouchedp.

3.3 int* const p

3.4 const int* const p

4.constKeyword-modified function return value scenarios
const int* test()
{
    
	static int a = 10;
	return &a;
}
int main()
{
    
	int* p = test();
	return 0;
}

这里的constThe meaning of modifying the return value of the function is to prompt the programmer to maintain it later,Do not make changes to its return value.

6.3 实现模拟strlen库函数

int my_strlen(const char* str)
{
    
    //计数器
	int count = 0;
	//保证指针有效性
	assert(str != NULL);
	//Judge the condition again*str='\0'时停止
	while (*str)
	{
    
		count++;
		str++;
	}
	return count;
}
int main()
{
    
	const char* p = "abcdef";
	int len = my_strlen(p);
	printf("len = %d\n", len);
	return 0;
}

7.Common programming errors

编译型错误:Just read the wrong message
链接型错误:一般是标识符名不存在或者拼写错误
运行时错误;Not easy to findbug,General debugging can solve it

We need to be good at finding problems,解决问题的人,Let your head fly at high speed to solve the problem,That's how you can become a great person,Still have to maintain an empty cup mentality,重复输入,高效输出.加油!

原网站

版权声明
本文为[playful green shaft]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091858050992.html