Visual Studio2022 Compiler Practical Debugging Skills

Table of contents

1. What is a bug

2. What is debugging?

3. Introduction to debug and release

4. Introduction to Windows Environment Debugging

4.1 Preparation for debugging environment

4.2 Learn shortcut keys

4.3 View the current information of the program during debugging

4.4 View memory information

5. If you write good (easy to debug) code

7. Common errors in programming


1. What is a bug

This is the first program "bug" that Grace Hopper found when running the program on the Markll machine in 1947, and it was posted in her notebook. It really is a bug. This small flattened moth was in the relay contacts of the Mark II computer, and it "stuck" the operation of the machine, resulting in an error in operation. It was the first computer program error . Later, people habitually called the problems of the program Bugs, and called the troubleshooting of the program Debug (deworming).
 

2. What is debugging?

When we write code again, don't just copy and paste. For the problems in the program, we need to solve them through debugging, not as shown in the figure below:

 Refusal-superstitious debugging!!!!
Debugging (English: Debugging / Debug), also known as debugging, is a process of discovering and reducing program errors in computer programs or electronic equipment.

Basic steps of debugging · Discover the existence of program errors:

  1. Locate errors by isolating, eliminating, etc.
  2. determine the cause of the error
  3. Propose a solution to correct the error
  4. Correct program errors and retest
     

3. Introduction to debug and release

Debug is usually called the debug version, it contains debugging information, and does not make any optimization, which is convenient for programmers to debug programs.

Release is called the release version, and it is often optimized to make the program optimal in code size and running speed, so that users can use it well.

#include<stdio.h>
int main()
{
    char *p = "hello xilanhua";
    printf("%s\n",p);
    return 0;
}

The result of the above code in the Debug environment is displayed

 The result of the above code in the Release environment is displayed 

 It can be found that the executable file under the Release version is much smaller than the executable file under the Debug version, indicating that the Release version has been optimized.

Take a look at the following code:

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

In Debug mode, in the x86(32-bit) environment, there will be an infinite loop. Because of:

Both i and the array arr are local variables, which are created in the stack area. The memory usage of the stack area is characterized by using the high address space first, and the array is arranged from the low address to the high address. There are two words between the VS compiler variables. section, so when the loop executes to arr[12], the value of i will be changed to 0, resulting in an infinite loop.

 If it is compiled in debug mode, the result of the program is an infinite loop. If it is compiled in release mode, the program does not have an infinite loop. So what is the difference between them? It is because of optimization.
 

4. Introduction to Windows Environment Debugging

Note: The Linux development environment debugging tool type gdb will be discussed later in the course

4.1 Preparation for debugging environment

Select the Debug option  in the environment to make the code debug normally

4.2 Learn shortcut keys

The most commonly used shortcut keys:
F5

Start debugging, often used to directly transfer to the next breakpoint. No breakpoint will let the program run to completion.

F9

Create breakpoints and cancel breakpoints The important role of breakpoints, you can set breakpoints anywhere in the program. In this way, the program can be stopped at any desired position, and then executed step by step.

F10

Process by process, usually used to process a process, a process can be a function call, or a statement.

F11

Statement by statement means to execute a statement each time, but this shortcut key can make our execution logic enter the function (this is the longest use).

CTRL+ F5

Start execution without debugging, if you want the program to run directly without debugging, you can use it directly.

4.3 View the current information of the program during debugging

View the value of a temporary variable:

After debugging starts, it is used to observe the value of the variable

 

4.4 View memory information

Start observing memory information after debugging starts

 

A lot of hands-on, try to debug, in order to make progress

- Must be proficient in debugging skills.
Beginners may spend 80% of their time writing code and 20% of their time debugging. But a programmer may spend 20% of his time writing programs, but 80% of his time debugging.
We're all talking about some simple debugging. In the future, there may be very complex debugging scenarios: debugging of multi-threaded programs, etc. · Use more shortcut keys to improve efficiency.

5. If you write good (easy to debug) code

Excellent code:

  • 1. The code runs fine
  • 2. Few bugs
  • 3. High efficiency
  • 4. High readability
  • 5. High maintainability
  • 6. Comments are clear
  • 7. Complete documentation

Common coding skills:

  • 1. Use the assert assertion, which will be used below
  • 2. Try to use const 
  • 3. Develop a good coding style
  • 4. Add necessary comments
  • 5. Avoid coding pitfalls. demonstration:

usage of const

When const modifies the pointer,
when const is placed on the left side of *, the content pointed to by the restricted pointer cannot be changed through the pointer variable, but the pointer variable itself can be changed. When
const is placed on the right side of *, the restricted pointer The variable itself, the pointer variable itself cannot be changed, but the content pointed to by the pointer can be changed through the pointer

#include<stdio.h>
int main()
{
	int m = 10;
	//const 可以修饰指针
	int n = 100;
	const int*  p = &m;//*p的值不可改变
	//int* const p = &m;  //p的指向不能改变
	//*p = 0;
	//p = &n;
	return 0;
}


Simulate the implementation of library functions: strcpy. Example:

#include<stdio.h>
#include<string.h>
#include<assert.h>//assert 断言

void my_strcpy(char* dest, const char* src)//const 使为了防止*src被修改
{
	//断言
	assert(dest != NULL);//条件为假报错,会直接提示是这里出错
	assert(src != NULL);

	char* ret = dest;

	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;//拷贝 \0

	//while (*dest++ = *src++);//等同于上面代码

	return ret;
}


int main()
{
	char arr1[] = "hello world";
	char arr2[20] = "*************";

	//会把原字符串的'\0'拷贝过来
	my_strcpy(arr2, arr1);//返回的是目的地的地址
	printf("%s\n", arr2);
	printf("%s\n", strcpy(arr2, arr1));//链式访问
	return 0;
}

7. Common errors in programming

Common misclassifications:

compile error

Look directly at the error message, double-click to locate and solve the problem. Or it can be done with experience. Relatively simple.

link error

link An error occurred during linking. Look at the error message, mainly find the identifier in the error message in the code, and then locate the problem. Usually the identifier name does not exist or is misspelled.

runtime error

With the help of debugging, locate the problem step by step. The most difficult.

Finally, today is the day of the first love and peaceful breakup, which will last for 78 days. I wish myself happiness every day in the future.

end of article

 

Guess you like

Origin blog.csdn.net/qq_72916130/article/details/130894578