Detailed explanation of C language assert macro

Preface: When we write code, we often find that there is always an assert (expression) in the code of some big cows. After studying on the Internet, the author also understands the relevant knowledge of assert. Assert is generally used to standardize code and avoid Unnecessary mistakes, I will share with you below.

1.Basic introduction of assert macro

  • prototype:

  • effect:

insert image description here

 To put it simply, if the value of espression is 0, or false (for example, the pointer is NULL), the abort function will be called to terminate the running of the program and tell you the specific error message. For example, if the judgment is 0, the following format output error message

2. Common usage

  • Check function arguments for errors (most commonly passing NULL)

        When passing function parameters, it is often the case of passing a pointer variable (address). If you do not pay attention, the situation of passing a null pointer will occur. In this case, the passed function parameters are wrong, so you need to add assert to avoid this. mistake.

        Code 1: A simple code

 

        Code 2: Implementation of my_strcpy

//简化代码   标准代码,要考虑到所有可能出现错误的地方,比如指针经常传递NULL
char* my_strcpy(char* dest, const char* src)
{
	assert(src);//等价于assert(src!=NULL)
	assert(dest != NULL);
	char* ret = dest;
	while (*dest++ = *src++);//后置加,先把赋值后的结果先被while使用,再++
	return ret;
}
int main()
{
	/*char arr1[] = "hello world";*/
	char *p=NULL;
	char arr2[] = "xxxxxxxxxxx";

	printf("%s\n", my_strcpy(arr2, p));
	return 0;
}
  • Check array index

 

/*定义一个全局的数组*/
static int my_array[10] = { 1,2,3,4,5,6,7,8,9,10 };
int get_array_index(int index)
{
	/*判断数组索引是否越界*/
	assert(index >= 0 && index < 10);//索引的值只能在0和9之间
	return my_array[index];
}
int main()
{
	/*目的:查找my_array数组中对应位置的元素,5就是查找第五个元素*/
	int x = get_array_index(5);
	int y = get_array_index(15);//超过索引,所以会报错

	return 0;
}

3. Precautions for assert macros

        1.#define NEDBUG

        We use assert mainly to find and reduce errors during programming, rather than to display errors on the client side or at runtime, so generally after the programmer finishes debugging, add #define NEDBUG to terminate all assert functions, that is to say, all The assert macros are all invalid. Why do you do this, mainly for the following reasons

  • assert is generally used to capture errors or exceptions that occur during programming, which will affect program performance and user experience in actual operation;
  • Reduce the size of the program after release, improve the running speed of the program, and reduce the occupied system space;
  • Avoid the leakage of sensitive data (because the error information is very detailed, including the file name and specific code location);

        2. Use must include the header file #include <assert.h>

4. Summary: assert can help us standardize the code and help programmers catch errors in time. Adding assert macros in appropriate places is a good code habit. I hope everyone can standardize their own code!

Guess you like

Origin blog.csdn.net/Mylvzi/article/details/130821734