Practical debugging skills for code

1. Basic steps of debugging
1. When the program is running, find out the existence of the program error and locate it
2. Mark the error
3. Determine the cause of the error
4. Propose a solution to correct the error
5. Corrected program, re-run, re-test
2. Debug and Release
▪Debug
Debug, also known as debug version, usually contains debugging information and does not make any optimization to facilitate programmers to debug the program, but when the system is executed, it will Generate a file with relatively large memory, as shown below:

The .exe file generated by Debug is 39KB in size, so why is it said that the file it generates is large? Read on.

▪Release
Release, also known as release version, as the name suggests, is the version released when a product is launched. It is generally optimized through various methods to make the size and running speed of the program optimal. When the system uses Release When executed, a relatively small file will be generated, as shown below:
The same code is 39KB when running on Debug, and 9KB when running on Release. Therefore, Release has the advantage of generating files that consume less memory, but it also has the disadvantage of being relatively small. What a great place.

#include<stdio.h>
int main() {
	int i = 0;
	int arr[10] = { 0 };
	for (i = 0;i <= 12;i++) {
		arr[i] = 0;
		printf("good\n");
	}
	return 0;
}

Before running the program, you can find an error by looking at the code, that is, the array out-of-bounds error, because only the length of the array is defined to be 10, but in the for loop, arr[10] and arr[11] are accessed , arr[12] may cause an exception in the program.
3. Use Debug to find the corresponding error.
According to our normal understanding, when the above code runs to i=10, arr[i]=0 will not succeed. Because the length of the array is only 10, the program should report an error message, but when running the program, it keeps looping endlessly. Therefore, we set a breakpoint on line 7 of the code and call the monitoring window in Debug mode to further analyze the changes in values ​​while the code is running.
When i=10,11, the numerical results of arr[i] are the same:

But when i=12, the value of arr[i] suddenly becomes:

When debugging further, the value of i suddenly changes to 1, and the value of arr[i] is 0:

Continuing with debugging, we can see that the value of i ranges from 0 to 11, the value of arr[i] is all 0, and when i=12, the value of arr[i] is still 12, which is the same as the above situation, and then go down When running, the result keeps looping, causing an infinite loop.
Then call the memory window in debugging mode to view the changes in the memory of the arr array and i during debugging.

It can be seen from the monitoring window and memory window that the addresses of arr[12] and i coincide, and when i=12, the program runs to arr[i]=0, that is, arr[12]=0, because their addresses The same, so from now on the value of i also changes to 0, which satisfies the loop condition, so the loop continues. Every time i=12, its value will be set to 0, thus creating an infinite loop.

This is a related question in the written test of Nice Company. In the process of learning code, 20% is syntax and 80% is debugging code. Debugging code is very important in our future work. Learning debugging skills will enable us to I have benefited a lot from my work and feel at ease!

Guess you like

Origin blog.csdn.net/whwwyshf/article/details/133057234