Practical debugging tips about MSVS

How to solve the bug problem? How to find bugs? ------debug

What is a bug? Where did the bug come from?

  The English meaning of bug is bedbug. The first bug in our computer was because a dead bedbug entered the computer and caused an error. Therefore, we call "computer error" a bug.

What is debugging?

Debugging, also known as debugging, is a process of discovering and reducing errors in computer programs or electronic equipment.

Basic steps for debugging

  • Discover the existence of program errors
  • Locate errors by isolating and eliminating them
  • Determine the cause of the error
  • Propose solutions to correct errors
  • Correct program errors and retest

Introduction to Debug and Release 

  •  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;
  • 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.

The debug version is used for our debugging, and the release version is used to serve customers.

1. Preparing the debugging environment

debug is a development environment that we can debug. We must first set it to debug, as shown in the figure:

2. Debugging shortcut keys

F10

  • F10: Process by process, usually used to process a process. A process can be a function call or a statement.

 

Step 2: Open the monitoring window

Then the monitoring window opens as follows: 

  •  Press f10 three times to run the program

Of course, we can also see the address of i through the monitoring interface. Just enter &i.

 

F9 + F5

  • F9: Create and cancel breakpoints
  • The function of breakpoint is to set a breakpoint anywhere in the program. By setting a breakpoint, the program can be temporarily executed at the desired position, and then the program can be temporarily executed. Next, 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
  • F5: starts debugging. It is often used to jump directly to the next breakpoint. It is usually used in conjunction with F9.
  • f9+f5 controls the running position of the program

The red dot below will appear. The red dot is the breakpoint we created.​ 

  • By pressing f5 again, we can jump the program directly to the location of the red dot. The previous assignment loop is skipped directly.

 

 F11

  • F11: Statement by statement, that is, executing one statement each time, but this shortcut key can make our execution logic enter the function. In the letter
  • Where a number 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.

When the arrow points to line 29, we press f11

You can now enter the function!

CTRL + F5

CTRL + F5:Start execution without debugging. If you want the program to run directly without debugging, you can use it directly.

3. View memory information


 4. Practice

Now comes the practical work of debugging!

Calculation 1! +2! +3! +...+n!

 But the calculated result is 15

 

 There is no problem at the moment, let’s continue to search

 

Then we continue 

 Finding bugs requires patience, don’t be anxious

 

Then we found the problem, ret should = 3! , why ret=12?​ 

Then let's look back

 

It turns out that we count it as 2! Finally, our ret did not change back to 1,

Instead, add 2! The value continues to be carried, and then there is ret=2! × 3! = 12

 Solution: Just change the position of int ret = 1;.

 

Guess you like

Origin blog.csdn.net/2302_79491024/article/details/133959565