C language debugging skills (debug) and problems that occur when the program is running

Table of contents

1. What is debugging?

1.Introduction to debugging

2.Debug and Release

3. Comparison between Debug and Release

2. How to debug

1. Introduce several debugging shortcut keys

2. Check the current information of the program during debugging

3. Common programming errors

1. Compilation error (the simplest)

2. Link type error

3. Runtime errors (most difficult)


The following content is all performed on VS2022

1. What is debugging?

1.Introduction to debugging

(1) What can be done during debugging?

Debugging (Debugging/Debug), also known as troubleshooting. If there are errors or bugs in our code, we can use debugging methods to find them.

(2) General steps of debugging

  • Discover the existence of program errors
  • Locate errors by means of isolation, elimination, etc.
  • Determine the cause of the error
  • Propose solutions to correct errors
  • Correct program errors and retest

2.Debug and Release

(1)Debug

  • Location in VS2022 

  • The meaning of Debug: After the code is run, no optimization will be performed on the program, which means that it is convenient for the program to debug the code and check for errors. The process of debugging the code is carried out in the Debug environment.

(2)Release

  • Location in VS2022

  •  The meaning of Release version: Release is also 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.

3. Comparison between Debug and Release

  • Look at a piece of code: obvious array out-of-bounds access
#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;
}
  • Run under Debug version

  • Run under Release version

2. How to debug

Must be in Debug version to debug

1. Introduce several debugging shortcut keys

(1)F5

[Function]:Start debugging, often used to jump directly to the next breakpoint. Often used with F9

(2)F9

[Function]:Create breakpoints and cancel breakpoints

Breakpoint: Press F9 with the mouse over a certain line to create a breakpoint for the job, and press it again to cancel (or use the left mouse button against the gray area)

(3)F10

[Function]:Process-by-process, usually used to process a process. A process can be a function call or a statement.

[Usage] When the code can be compiled, click F10 and the code will enter the debugging panel, which can also be used to control the execution of statements one by one. We can take a look at the page after pressing F10 to adjust it.

  • When F10 is pressed, the yellow arrow will go down. Press to walk one line at a time. When a function call is encountered, the entire function process will be walked directly, as follows:

(4) F11

[Function]: Statement by statement, that is, executing one statement each time, but this shortcut key can make our execution logic enter the inside of the function (this is the most commonly used).

[Usage] After the code is debugged, use F11 to control the code to go down one by one; you can also make it enter the function.

(5)ctrl+F5

[Function]: Start execution without debugging, just to let your code run. The entire page below is the result of the ctrl+F5 code running.

2. Check the current information of the program during debugging

(1) View the value of temporary variables (most commonly used)

  • To open a window

  • After clicking on a window

We use the following code to demonstrate:

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
int add(int x,int y) {
	return x + y;
}
int main() {
	int a = 10;
	int b = 20;
	int c = add(a,b);
	printf("%d\n",c);
	return 0;
}
  • Next we look at the variables a, b, c

 

  • After execution, the following code is the same

  • View the value after the array call (important)

First look at the following piece of code: the address of the array needs to be passed into the function as a parameter. Debug it below

#include<stdio.h>
void test(int arr[],int len) {
	int i = 0;
	for (i = 0; i < len;i++) {
	
		printf("%d ",arr[i]);
	}
}
int main() {
	int arr[] = {1,2,3,4,5,6,7,8,9,10};
	int len = sizeof(arr) / sizeof(arr[0]);
	test(arr,len);
	return 0;
}
  • The code is before entering the function

  • After the code enters the function, first look at the error observation method

  • Correct approach (key points)  

(2) View memory information

  • In this step, you can check the address of the variable, especially when it comes to pointers, this is very important.
  • To open the memory window

We use the following code to demonstrate:

#include<stdio.h>
int main() {
	int a = 10;
	int* p = NULL;
	p = &a;
	printf("%d\n",*p);
	return 0;
}
  • View variable a and pointer variable p

 

(3) View the call stack (less used)

  • Open the call stack window

  • Use this window: clear the calling relationship of the reaction function and the location of the current call.

(4) View assembly information (useful when viewing the establishment of function stack frame space)

[First method] After debugging starts, right-click the mouse and select options

  • open window

  • After entering the window

【Second method】

  • open window

  • After entering the window (same content)

(5) View register information (less used)

  • window path

  • After opening the window: you can view the register usage information of the current running environment

3. Common programming errors

We know that turning source code into a program requires compilation (preprocessing, compilation, assembly), linking, and running. The errors that occur at each stage correspond to each type of error.

1. Compilation error (the simplest)

  • Errors that occur during the compilation phase are also called syntax errors; such as typographical errors in variable names, missing semicolons, etc.
  • Method: Look directly at the error message (double-click) to solve the problem. Or you can do it with experience
  • Example:

2. Link type error

  • During the linking phase, if you forget to write the header file, unresolved external symbols will be displayed; or the identifier is written incorrectly or does not exist.
  • Example 1

  • Example 2 

3. Runtime errors (most difficult)

  • Errors after the code is run: If the running result is different from the expected result, an infinite loop occurs, etc.
  • Method: You can only use debugging to find it step by step


If you want to reduce bugs, you must be careful. If you want to find bugs in time, you must debug and practice more.

Guess you like

Origin blog.csdn.net/2301_77053417/article/details/134097252