VS debugging skills (all dry goods)

What is debugging?

Debugging is a means that we need to find and solve problems when there is a problem running the code. This method is called debugging (it is also the only way for Xiaobai to become a big boss )

Under what circumstances do you use debugging?

There are many cases of code problems, among which logic errors are the most common case, which is often the most uncomfortable, because people who write code often interpret the code according to their ideal process, but the fact is that running The results are very different, so when we encounter logic errors, we need to use debugging to understand the whole process of the code running, and then find and solve the problem .

Debug和Release

When VS writes code, we can see the two options of Debug and Release.
insert image description here

Debug

Debug is called the debug version. In this environment, VS will not perform any optimization on the code, that is to say, the code is what it is, so the programmer can find out the bugs in the code by means of debugging in this environment. fundamental problem.

Release

Release is called the release version, that is, the programmer needs to apply the code to practice after writing the code. more convenient. Using Release generally means that the programmer finishes writing the code, and then tests the program until the quality of the programmer meets the standards for delivery to users. At this time, it will be set to release.

VS debugging shortcut key

The following is the VS debugging shortcut key I compiled (must be the Debug version to use)

F9: Create breakpoint and cancel breakpoint Breakpoint: Breakpoint can be set anywhere
in the program , setting a breakpoint can make the program execute to the desired position and temporarily execute , in simple terms, it is the program before the breakpoint It will be executed, but the program after the breakpoint will not be executed temporarily (unless you use F10 and F11) Conditional breakpoint: Similar to the if statement, it will only break when the condition is met .

F5: Start debugging, often used to jump directly to the next breakpoint.
For example, we set two breakpoints. When we start debugging, if we are at the first breakpoint, then when we press F5 , we will jump directly to the second breakpoint, of course, if we start at the second breakpoint and press F5, then we will jump to the first breakpoint again, that is, we can understand the breakpoint as Portal, we can jump between these two breakpoints by pressing F5

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, execute a statement each time , but this shortcut key can make our execution logic enter the function . In 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, you can directly complete the function call

CTRL+F5: Execute code directly without debugging

Monitoring and Memory Watching

When debugging, we need to observe the changes of variables in the code and the changes of memory, so we need to use supervisors and memory observations.
for example

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

monitor

After starting debugging (otherwise the monitoring cannot be found), in the menu bar [Debug]->[Window]->[Monitor], just enter the object to be observed.

Memory

If the monitoring window is not careful enough, you can also observe the storage of variables in memory
[Debug] -> [Window] -> [Memory]

Debug example

1:求1!+2!+3!+4!+...10!的和,请看下⾯的代码:
#include <stdio.h>
//写⼀个代码求n的阶乘
int main()
{
    
    
    int n = 0;
    scanf("%d", &n);
    int i = 1;
    int ret = 1;
    for (i = 1; i <= n; i++)
    {
    
    
        ret *= i;
    }
    printf("%d\n", ret);
    return 0;
}
//如果n分别是1,2,3,4,5...10,求出每个数的阶乘,再求和就好了
//在上⾯的代码上改造
int main()
{
    
    
    int n = 0;
    int i = 1;
    int sum = 0;
    for (n = 1; n <= 10; n++)
    {
    
    
        for (i = 1; i <= n; i++)
        {
    
    
            ret *= i;
        }
        sum += ret;
    }
    printf("%d\n", sum);
    return 0;
}
//运⾏结果应该是错的?


2:在VS2019、X86、Debug 的环境下,编译器不做任何优化的话,下⾯代码执⾏的结果是啥?
#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;
}





Guess you like

Origin blog.csdn.net/2301_79178723/article/details/132265823