[VS Practical Debugging Tips]

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

提示:这里可以添加本文要记录的大概内容:

For example: With the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This article introduces the basic content of machine learning.


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is a bug?

The original meaning of "bug" is "insect" or "bug". Now it generally refers to some undiscovered defects or problems hidden in computer systems or programs, referred to as program vulnerabilities.
The founder of “Bug”, Grace Murray Hopper, was a computer expert working for the U.S. Navy. On September 9, 1947, Grace Murray Hopper set 17,000 relays on the Harvard Mark II After programming, the technicians were running the machine when it suddenly stopped working. So they climbed up to find out why and found a moth between the contacts of a set of relays inside the huge computer, apparently due to The moth was attracted by light and heat, flew to the contact, and was killed by the high voltage. So in the report, Heber used tape to stick the moth on it, and used "bug" to mean "a bug in the computer program." "Error", the term "Bug" is still used today.
Insert image description here

2. What is debugging?

When we find a problem in the program, the next step is to find the problem and fix it.
This process of finding problems is called debugging, which is called debug< in English. /span>
To debug a program, you must first admit that a problem has occurred, and then use various means to locate the problem. It may be debugging step by step, or it may be to isolate and shield the code to find the problem. Find the location of the error, determine the cause of the error, fix the code, and retest. (eliminate bug) means.

3, debug release

Insert image description here
When writing code on VS, you can see that there are two options: debug and release. What do they mean?

Debug is usually called the debug version: It contains debugging information and does not make any optimization, making it easier for programmers to debug programs; programmers need to debug regularly when writing code. Code, set this to debug, so that the compiled version will produce a debug version of the executable program, which contains debugging information and can be debugged directly.

Release is called a release version: It often carries out various optimizations to make the program optimal in terms of code size and running speed so that users can use it well. . When the programmer finishes writing the code, the program is tested again until the programmer's quality meets the standards delivered to the user. At this time, it will be set to release. The compiled version is the release version of the executable program. This version is the user's used, no need to include debugging information, etc.

Insert image description here
By comparison, you can see that the size of the executable file generated by compiling the same piece of code is that the release version is obviously smaller, while the debug version is significantly larger.

4. VS debugging shortcut keys

So how do programmers debug code?

4.1 Environment preparation

The first is the preparation of the environment. We need a development environment that supports debugging. When we use VS in class, we should set VS to debug, as shown in the figure:
Insert image description here

4.2 Debugging shortcut keys

Some of the most commonly used shortcut keys for debugging:

F10: Process by process, usually used to process a process. A process can be a function call or a statement.
F11: Statement by statement, that is, executing one statement each time, but this shortcut key can make our execution logic Enter inside the function. At the place where the function is called, if you want to enter the function to observe the details, you must use F11. If you use F10, the function call is completed directly.
CTRL + F5: Start execution without debugging. If you want the program to run directly without debugging, you can use it directly.

F5: Start debugging, often used to jump directly to the next breakpoint, usually used in conjunction with F9.
F9: Create breakpoints and cancel breakpoints
The function of breakpoints is You can set a breakpoint at any location in the program. By setting a breakpoint, the program can be temporarily executed at the desired location. Then we can use the shortcut keys F10 and F11 to observe the execution details of the code.
Conditional breakpoint: The breakpoint is triggered only when this condition is met

For more information about VS shortcut keys, please click here

5. Monitoring and memory observation

During the debugging process, if we want to observe the values ​​of variables in the context during code execution, what methods are there? The prerequisite for these observations must be observation after starting debugging, such as:

#include <stdio.h>
int main()
{
    
    
 int arr[10] = {
    
     0 };
 int num = 100;
 char c = 'w';
 int i = 0;
 for (i = 0; i < 10; i++)
 {
    
    
 arr[i] = i;
 }
 return 0;
 }

5.1 Monitoring

After starting debugging, go to [Debug]->[Window]->[Monitor] in the menu bar, open any monitoring window, and enter the object you want to observe.
Open the monitoring window:
Insert image description here
Observe in the monitoring window:
Insert image description here

5.2 Memory

If you don’t look carefully enough in the monitoring window, you can also observe the storage of variables in memory by going to [Debug]->[Window]->[Memory]

Insert image description here
Observe data in the memory window:
Insert image description here

Insert image description here
After opening the memory window, enter: arr, &num, &c in the address bar, and you can observe the data at that address.

Insert image description here

6. Debugging example 1

Insert image description here
Insert image description here

7. Debugging example 2

In VS2019, X86, and Debug environments, if the compiler does not perform any optimization, what is the result of executing the following code?
Insert image description here
The program runs in an infinite loop. Debug to see why?
Insert image description here
The memory layout of the above program can be debugged as follows:
Insert image description here
1. The usage habit of stack area memory is from high address to low address, so the address of variable i is larger. The overall address of the arr array is an address smaller than i.

2. The storage of arrays in memory is: as the subscript increases, the address changes from low to high.
So based on the code, you can understand why the code layout above is.
If it is the memory layout above, as the array subscript grows, it may overwrite i if it crosses the boundary later, which may cause an infinite loop.

There must be students here who have questions: Why are there exactly two integer spaces left between the i and arr arrays? This is indeed a coincidence. The size of the free space in the middle may be different under different compilers. The memory allocation and address allocation of these variables in the code are specified by the compiler, so there are differences between different compilers. There is a difference. So this topic is related to the environment.
Insert image description here
From this understanding, we can realize the importance of debugging. Only by debugging can we observe the details of the internal execution of the program, just like a doctor doing B-ultrasound or CT on a patient.

8. Classification of common programming errors

8.1 Linked errors

Look at the error message, mainly find the identifier in the error message in the code, and then locate the problem. Usually because:

• Identifier name does not exist
• Spelling error
• Header file not included
• Referenced library Does not exist

Insert image description here

8.1 Compilation errors

Compilation errors are generally grammatical errors. You can usually find some clues by looking at the error message. Double-clicking the error message can also initially jump to the location of the code error. Compilation errors will become fewer and fewer as you become more proficient in the language, and they will be easier to solve.
Insert image description here

8.3 Runtime errors

Runtime errors are ever-changing, and you need to use debugging to gradually locate the problem. Debugging solves runtime problems.

Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/133895342