C language practical debugging skills (1)

Bug in English means bedbug, moth, let me show you a picture

How do we write code?
And how to troubleshoot the problem?
Are these the status quo that everyone usually writes code? ? ?
What is 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 for commissioning
detect the presence of programming errors
Locate errors by isolating, eliminating, etc.
determine the cause of the error
Propose a solution to correct the error
Correct program errors and retest

Debug (debug version), Release (release version), the following will tell you the detailed concept

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 , which is often optimized to make the program optimal in code size and running speed
, so that users can use the
The release version of the code does not contain debugging information, so we cannot debug by pressing F10 on VS2019

 

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

Debug and Release disassembly display comparison:

 

 

When debugging, we should learn the corresponding shortcut keys, let's take a look in detail
Let's press and hold F9 first (click the line where you think there may be a mistake, then right-click the mouse and click to cut into the breakpoint) to switch the breakpoint, and press F5 to jump directly to your F9 breakpoint just now place, and then cooperate with F10 to debug and execute from the breakpoint, which saves a lot of unnecessary steps
Look at this case, right-click the mouse to add a breakpoint, there is a condition window inside, open and set the conditions you need
Here are some shortcut keys for you
The most commonly used shortcut keys:
F5
Start debugging, often used to jump directly to the next breakpoint
F9
Create 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 most
long-term use)
CTRL + F5
Start execution without debugging, if you want the program to run directly without debugging, you can use it directly
View the current information of the program during debugging
If F9, F10, and F11 do not work on your computer, it is a problem with the Fn key. Just turn off Fn.
Or Fn+F10   Fn+F5 Fn+F9 Fn+F11
3.3.1 Check the value of the temporary variable
Used to observe the value of a variable after debugging has started

After debugging, click monitor (any window is enough), see the figure below 3.3.2 View memory information      After debugging starts, it is used to observe memory information 3.3.3 View call stack

On VS2013, I tried it, press and hold F10 to debug, and then click on the debug window, and then click on the call stack. I have talked about the creation and destruction of the function stack frame in detail in the two blogs. You can go to see Take a look and review until the main function is out

3.3.4 View assembly information

After debugging starts, there are two ways to go to assembly. The first way: right-click the mouse and select [Go to disassembly]

The second way can switch to assembly code

3.3.5 View register information
4. 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
program, but 80% of the time is in debugging
All we're talking about is some simple debugging
There may be very complex debugging scenarios in the future: debugging of multi-threaded programs, etc.
Use more shortcut keys to improve efficiency
Let's take two examples
look at the code
At this time, if we are 3 , we expect to output 9 , but the actual output is 15
This factorial is wrong. I will debug it for everyone to see that it will take up a lot of space in the article. Come down and try the above method
There will be different effects on different compilers. The above picture has given you a detailed explanation
No error was reported when i <= 12, why no error was reported, because he has been busy with an endless loop and has no time to report an error
Of course, when i <= 14, it is a matter of course that there will always be an infinite loop. I hope everyone can understand

There is another situation that i <= 11. There is no infinite loop, but an error will be reported. The reason is that I have not crossed the boundary, so it does not constitute an infinite loop. It is just an error. I hope everyone can understand 
At the end of this chapter, the next article will tell you about the last two parts, detailed support! ! !

 

 

Guess you like

Origin blog.csdn.net/fjj2397194209/article/details/131196740