• assert函数实验


    前言       

    1. #include
    2. void assert(scalar expression);

            assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。

    NAME

            assert - abort the program if assertion is false

    DESCRIPTION
           If  the  macro NDEBUG was defined at the moment was last included, the macro assert() generates no code, and hence does nothing at all.  Otherwise, the macro assert() prints an error mes‐
           sage to standard error and terminates the program by calling abort(3) if expression is false (i.e., compares equal to zero).

           The purpose of this macro is to help programmers find bugs in their programs.  The message "assertion failed in file foo.c, function do_bar(), line 1287" is of no help at all to a user.

            如果宏NDEBUG是在最后被包含的时候定义的,宏assert()不会生成任何代码,因此什么也不做。否则,宏assert()将向标准错误输出错误消息,如果表达式为false(即比较等于零),则调用abort(3)终止程序。这个宏的目的是帮助程序员发现程序中的bug。消息“断言失败在文件foo.c,函数do_bar(),第1287行”对用户毫无帮助

    RETURN VALUE
           No value is returned.
     

    一 例程1:assert基本使用

    代码:

    1. #include
    2. #include
    3. #define DEBUG_INFO(format,...) printf(\
    4. "line=%d:"format"\n",__LINE__,\
    5. ##__VA_ARGS__)
    6. int main(int argc,char *argv)
    7. {
    8. int i = 0,j = 1;
    9. DEBUG_INFO("i = 0");
    10. assert(i == 0);
    11. DEBUG_INFO("j = 1");
    12. assert(j == 0);
    13. printf("bye\n");
    14. return 0;
    15. }

    运行结果:从输出结果可知,i初始值是0,assert(i == 0)正常执行,i的值赋值为1,第二个assert(i == 0)就报错了。报错后,assert内部自动调用abort,退出程序。

    1. csdn@ubuntu:~$ ./a.out
    2. line=11:i = 0
    3. line=13:j = 1
    4. a.out: assert.c:14: main: Assertion `j == 0' failed.
    5. 已放弃 (核心已转储)
    6. csdn@ubuntu:~$

    二 例程:NDEBUG验证

    验证默认环境是否定义了NDEBUG:修改代码如下:

    1. #include
    2. #include
    3. #define DEBUG_INFO(format,...) printf(\
    4. "line=%d:"format"\n",__LINE__,\
    5. ##__VA_ARGS__)
    6. int main(int argc,char *argv)
    7. {
    8. int i = 0,j = 1;
    9. #ifdef NDEBUG
    10. DEBUG_INFO("NDEBUG is defined");
    11. #else
    12. DEBUG_INFO("NDEBUG is not defined");
    13. #endif
    14. DEBUG_INFO("i = 0");
    15. assert(i == 0);
    16. DEBUG_INFO("j = 1");
    17. assert(j == 0);
    18. printf("bye\n");
    19. return 0;
    20. }

    运行结果:又结果可知,NDEBUG默认未被定义

    1. csdn@ubuntu:~$ ./a.out
    2. line=15:NDEBUG is not defined
    3. line=17:i = 0
    4. line=19:j = 1
    5. a.out: assert.c:20: main: Assertion `j == 0' failed.
    6. 已放弃 (核心已转储)
    7. csdn@ubuntu:~$

    三 自定义NDEBUG

    自定义的NDEBUG必须在#include 的前面,否则是无效的。

    1. #define NDEBUG
    2. #include
    3. #include
    4. #define DEBUG_INFO(format,...) printf(\
    5. "line=%d:"format"\n",__LINE__,\
    6. ##__VA_ARGS__)
    7. int main(int argc,char *argv)
    8. {
    9. int i = 0,j = 1;
    10. #ifdef NDEBUG
    11. DEBUG_INFO("NDEBUG is defined");
    12. #else
    13. DEBUG_INFO("NDEBUG is not defined");
    14. #endif
    15. DEBUG_INFO("i = 0");
    16. assert(i == 0);
    17. DEBUG_INFO("j = 1");
    18. assert(j == 0);
    19. printf("bye\n");
    20. return 0;
    21. }

    执行结果:定义了NDEBUG后,就不会再退出程序了。

    1. csdn@ubuntu:~$ ./a.out
    2. line=13:NDEBUG is defined
    3. line=17:i = 0
    4. line=19:j = 1
    5. bye
    6. csdn@ubuntu:~$

    四 使用assert判断多个条件

    修改代码如下所示:

    1. //#define NDEBUG
    2. #include
    3. #include
    4. #define DEBUG_INFO(format,...) printf(\
    5. "line=%d:"format"\n",__LINE__,\
    6. ##__VA_ARGS__)
    7. #define DEBUG
    8. int main(int argc,char *argv)
    9. {
    10. int i = 0,j = 1;
    11. #ifdef NDEBUG
    12. DEBUG_INFO("NDEBUG is defined");
    13. #else
    14. DEBUG_INFO("NDEBUG is not defined");
    15. #endif
    16. DEBUG_INFO("i = 0");
    17. assert(i == 0);
    18. DEBUG_INFO("j = 1");
    19. assert(i == 0 || j == 0);
    20. assert(i == 0 && j == 0);
    21. printf("bye\n");
    22. return 0;
    23. }

    运行结果:这说明assert函数也是可以判断多个条件的。

    1. csdn@ubuntu:~$ ./a.out
    2. line=15:NDEBUG is not defined
    3. line=17:i = 0
    4. line=19:j = 1
    5. a.out: assert.c:21: main: Assertion `i == 0 && j == 0' failed.
    6. 已放弃 (核心已转储)
    7. csdn@ubuntu:~$

    小结

            实践,检验知识。

  • 相关阅读:
    RFID防盗安全门,自助借还书机,让图书馆发展进入新的里程碑
    py13_Python 的输入以及流程控制之分支条件判断结构
    BUUCTF-WEB-[ACTF2020 新生赛]Includel
    30.10.3 密码管理
    NPDP考试需要携带什么?文具携带说明
    实验室信息管理系统 LIMS 有哪些核心功能?
    服务器选择多大的带宽比较合适,如果遇到攻击了该怎么办
    javascript 使用vue实现全选和取消全选
    聊聊并发编程——多线程之synchronized
    yolov5的onnx推断示例和思路记录(包含detect.py的最新源码解读)
  • 原文地址:https://blog.csdn.net/yueni_zhao/article/details/127786214