最近调试程序,发现ST的固件库中到处都是assert_param(),不清楚其编译结果,感觉这个“校参”过程很影响效率
 在stm32f10x_conf.h中找到其源代码:
 /* Exported macro ------------------------------------------------------------*/
 #ifdef DEBUG
 /*******************************************************************************
 * Macro Name : assert_param
 * Description : The assert_param macro is used for function's parameters check.
 * It is used only if the library is compiled in DEBUG mode. 
 * Input : - expr: If expr is false, it calls assert_failed function
 * which reports the name of the source file and the source
 * line number of the call that failed. 
 * If expr is true, it returns no value.
 * Return : None
 *******************************************************************************/ 
 #define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__))
 /* Exported functions ------------------------------------------------------- */
 void assert_failed(u8* file, u32 line);
 #else
 #define assert_param(expr) ((void)0)
 #endif /* DEBUG */
 这里有这样的疑问:
 1)这个“DEBUG”的宏在什么地方定义?编译器的OPTIONS?还是自己随便在哪定义,比如在main.c中?
 2)((void)0)是什么意思,编译的结果是什么样的?
 3)void assert_failed(u8* file, u32 line);这个函数应该是在外部定义吧?
 网友回答:
 这是一种常见的软件技术,可以在调试阶段利用编译器的检验功能,帮助程序员始终选择有效的参数。
 所谓有效的参数是指满足规定范围的参数,比如某个参数的取值范围只能是小于3的正整数,如果给出的参数大于3,则这个assert_param()可以在编译时报告错误,使程序员可以及时发现错误,而不必等到运行程序时,因为程序运行错误而大费周折。
 它确实在程序的运行上牺牲了效率(但只是在调试阶段),但在项目的开发上却帮助你提高了效率。
 当你的项目开发成功,使用release模式编译之后,所有的assert_param()检验都消失了,不会影响最终程序的运行效率。
 用户需要在main.c文件中实现assert_failed函数
 #ifdef USE_FULL_ASSERT
 /**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * @param file: pointer to the source file name
 * @param line: assert_param error line source number
 * @retval None
 */
 void assert_failed(uint8_t* file, uint32_t line)
 { 
 /* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* Infinite loop */
 while (1)
 {
 }
 }
 #endif