zoukankan      html  css  js  c++  java
  • #include <assert.h>

    assert宏

    适用于软件测试、调试、排错

    被除数不能为0,assert可以用于检测被除数是否为0

     1 #define _CRT_SECURE_NO_WARNINGS
     2 
     3 //#define NDEBUG//关闭静态断言,必须放在头文件上方
     4 
     5 #include <stdio.h>
     6 #include <stdlib.h>
     7 #include <assert.h>//静态断言的头文件
     8 
     9 main()
    10 {
    11     double db1, db2;
    12     
    13     scanf("%lf%lf", &db1, &db2);
    14 
    15     printf("db1=%f,db2=%f
    ", db1, db2);
    16 
    17     assert(db2 != 0);//db2 != 0成立就是正常
    18 
    19     printf("%lf/%lf=%lf
    ", db1, db2, db1 / db2);
    20             
    21     system("pause");
    22 }

    malloc分配内存失败,返回NULL,assert可以用于检测指针是否NULL

     1 #define _CRT_SECURE_NO_WARNINGS
     2 
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 #include <assert.h>//静态断言的头文件
     6 
     7 main()
     8 {
     9     char *p = (char *)malloc(sizeof(char));
    10     assert(p != NULL);//malloc分配内存失败的时候返回NULL
    11 
    12     *p = 'h';
    13     
    14     printf("%c
    ", *p);
    15 
    16     system("pause");
    17 }

    如果要关闭静态断言,在头文件上方,加上

    1 #define NDEBUG//关闭静态断言,必须放在头文件上方
  • 相关阅读:
    python简单接口的测试(随机数等)
    关于数据库的去重+导入导出参数
    找到并杀死一个软件开启的进程
    blinker库
    HTTP状态码
    一致性哈希算法
    celery
    项目部署
    redis更多
    functools模块
  • 原文地址:https://www.cnblogs.com/denggelin/p/5544410.html
Copyright © 2011-2022 走看看