5051|1

2606

帖子

0

TA的资源

五彩晶圆(初级)

楼主
 

关于assert_param的疑惑 [复制链接]

最近调试程序,发现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
此帖出自stm32/stm8论坛

最新回复

能不能再说详细一点,STM8中也是有这个东西  详情 回复 发表于 2016-4-17 19:33
点赞 关注
个人签名工程 = 数学+物理+经济
 

回复
举报

613

帖子

2

TA的资源

纯净的硅(初级)

沙发
 
能不能再说详细一点,STM8中也是有这个东西
此帖出自stm32/stm8论坛
 
个人签名我是一头搞电子的猪,猪是一种好色的动物,猪八戒就是代表.       
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
艾睿电子& Silicon Labs 有奖直播 | 全新蓝牙信道探测:从技术创新到实际应用
直播时间:3月12日(周三)上午10:00
直播奖励:多功能榨汁机、蓝牙音箱、手机支架

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表