[C language] common debugging skills - entering the code

  • What is a bug?
  • What is debugging? How important is debugging capability?
  • A brief introduction to debug and release
  • Introduction to debugging under windows Environment
  • Some examples of debugging
  • How to write code that is easy to debug?
  • Common errors in C programming
  • Simulate the implementation of strcpy and strlen library functions

Test environment: vs2022

1, What is a bug?

The moth that was first found to cause a computer error was also the first computer program error.

2, What is debugging? How important is debugging capability?

All things that happen must have traces to follow. If there is a clear conscience, there is no need to cover up and there are no signs. If there is a guilty conscience, there must be signs. The more signs, the easier it is to follow the vine. This is the way of reasoning. Following this path down the river is a crime, and going up the river is the truth. Every excellent programmer is an excellent detective. When encountering a bug, every debugging is a process of trying to solve the case

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

Basic steps of debugging

  • The existence of program errors was found
  • Locate errors by isolation, elimination, etc
  • Determine the cause of the error
  • Propose solutions to correct errors
  • Correct the program errors and retest

3, A brief introduction to debug and release

Debug is usually called debugging version. It contains debugging information and does not make any optimization, which is convenient for programmers to debug programs.

Release is called release version. It is often optimized to make the program optimal in code size and running speed, so that users can use it well.

Code demonstration:

#include <stdio.h>
int main()
{
	int arr[10] = { 0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		arr[i] = i + 1;
	}
	for (i = 0; i < sz; i++)
	{
		printf("%d ",arr[i]);
	}
	return 0;
}

So we say that debugging is a process of finding potential problems in the code in the environment of the Debug version.

IV. introduction to debugging under windows Environment

  1. Preparation of debugging environment: select the debug option in the environment to make the code debug normally.     

     2. Common debugging shortcut keys (skilled)

F5: start debugging, which is often used to directly adjust to the next breakpoint.

F9: the important role of creating and canceling breakpoints. Breakpoints can be set anywhere in the program. In this way, the} program can stop executing at the desired position, and then execute step by step Press F9, then F5.

F10: procedure by procedure. It is usually used to process a procedure. A procedure can be a function call or a statement.

F11: execute one statement each time statement by statement. This shortcut key can make our execution logic enter the function (the most commonly used).

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

After debugging starts, you can see the relevant information of the window

     3. View the current information of the program during debugging

Code demonstration:

① view the value of temporary variable: it is used to observe the value of variable after debugging.

② view memory information: used to observe memory information after debugging.

One hexadecimal digit is four binary bits, and two hexadecimal digits are eight binary bits (one byte)

③ View the call stack: the call logic of the function is returned

Code demonstration:

void test2()
{
	printf("hehe\n");
}
void test1()
{
	test2();
}
void test()
{
	test1();
}
int main()
{
	test();
	return 0;
}

Some examples of debugging

Implementation code: seek 1+ 2!+ 3! ...+ n! ; Overflow is not considered.

Problem code:

int main()
{
	int n = 0;
	scanf("%d", &n);
	int i = 0;
	int ret = 1;
	int sum = 0;
	int j = 0;
	for (j = 1; j <= n; j++)
	{
		for (i = 1; i <= j; i++)
		{
			ret = ret * i;
		}
		sum = sum + ret;
	}
	printf("%d\n", sum);
}

Error analysis by debugging:

 

Correct code:

Running error: debugging solves running errors
solve the problem:
What should be the result - the problem is found when it is found that it does not meet the expectation during debugging

Example 2: what is the running result of the following code?

int main()
{
	int i = 0;
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	for (i = 0; i <= 12; i++)
	{
		
		arr[i] = 0;
		printf("hehe\n");
    }
	return 0;
}

Analysis by commissioning method:

Brief analysis:

  1. i and arr are local variables, which are placed on the stack
  2. The usage habit of stack memory is to use high address space first, and then low address space
  3. As the subscript of the array increases, the address changes from low to high
  4. When the array is accessed by subscript, it is possible to overwrite i as long as it crosses the boundary appropriately
  5. Once i is covered, it may lead to dead circulation
  6. Reserved space under different compilation environments: VC6 0 shaping; gcc 1 shaping vs 2 shaping

Improvement Code:

int main()
{
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int i = 0;
	//0 - 9
	for (i = 0; i <= 12; i++)//It has been in a dead cycle and has no chance to report errors
	{
		// If I < = 11 exceeds the boundary but does not cause a dead cycle, an error will be reported
		arr[i] = 0;
		printf("hehe\n");
	}
	return 0;
}

Prevent errors: reduce errors through coding techniques

6, How to write code that is easy to debug?

Excellent code:

  1. The code is running normally
  2. Few bug s
  3. efficient
  4. High readability
  5. Maintainability high
  6. Clear notes
  7. Complete documentation

Common coding techniques:

  1. Using assert
  2. Try to use const
  3. Develop a good coding style
  4. Add the necessary comments
  5. Avoid coding traps

7, Common errors in C programming

Common misclassification:

Compilation error: directly look at the error prompt (double click) to solve the problem It can be done with coding experience, which is relatively simple

Link error: look at the error message, mainly find the identifier in the error message in the code and locate the problem Generally, the identifier name does not exist or is misspelled

Runtime error: it is relatively difficult to locate the problem step by step with the help of debugging

Warm tips: be a conscientious person and accumulate troubleshooting experience

8, Simulate the implementation of strcpy and strlen library functions

int main()
{
	char arr1[20] = "xxxxxxxxxxxx";
	char arr2[] = "hello";
	//strcpy(arr1,arr2);
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

Method 1:

#include <string.h>
void my_strcpy(char* dest, char* src)
{
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;
}

Code optimization in method 1:

void my_strcpy(char* dest, char* src)
{
	while (*src != '\0')
	{
		*dest++ = *src++;
	}
	*dest = *src;
}

Note: * the priority of the dereference operator is higher than that of the + + operator. The assignment operation is carried out first, and then the self increment is independent of the self increment order

Method 2:

void my_strcpy(char* dest, char* src)
{
	while (*dest++ = *src++)
	{
		;
	}
}

Code brief analysis:

  1. *dest++ = *src + + compares the ASCII code value of characters. 0 is false, non-0 is true, and the ASCII code value of '\ 0' is 0
  2. 0 is false and does not enter the while loop If not 0 is true, enter the loop
  3. The function of empty statement is to copy '\ 0' and stop the loop

Keywords: C Back-end

Added by Cleibe on Wed, 19 Jan 2022 20:27:45 +0200