assert的实现和用法

assert很多时候到会用到,下面了解下assert的实现和用法

在C标准库中,assert的实现是用宏来实现的,并不是用函数,它在#include<assert.h>这标准C头文件

1、实现:宏assert是如何实现的呢?可以查看assert.h的代码,我查看的是mingw中的代码

#undef assert

#ifdef    __cplusplus
extern "C" {
#endif

#ifdef NDEBUG
/*
 * If not debugging, assert does nothing.
 */
#define assert(x)    ((void)0)

#else /* debugging enabled */

/*
 * CRTDLL nicely supplies a function which does the actual output and
 * call to abort.
 */
_CRTIMP void __cdecl __MINGW_NOTHROW _assert (const char*, const char*, int) __MINGW_ATTRIB_NORETURN;

/*
 * Definition of the assert macro.
 */
#define assert(e)       ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

#endif    /* NDEBUG */

#ifdef    __cplusplus
}
#endif

很明显的看到 #define assert(e)  ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

assert()它就是一个宏,如果表达式e为真,则为(void)0;表达式e为假,则调用_assert(#e, __FILE__, __LINE__)这函数

_assert(#e, __FILE__, __LINE__)具体实现类似以下的实现

 void _assert(const char *mesg, const char *file, int line)
 {
       printf("%s, %s, %d\n", mesg, file, line);
       abort();
 }

所以当表达式e为假的时候,函数abort()被用来终止程序运行.

2、用法

一般在在源文件中加上

#include <assert.h>或者

#undef NDEBUG

#include <assert.h>

取消的时候用

#define NDEBUG

#include <assert.h>  -#define NDEBUG应在#include <assert.h>前头, 这样才是正确的.看源码就知道了

assert(表达式);

示范: int a = 1; assert(a);

时间: 2024-08-08 06:21:06

assert的实现和用法的相关文章

Spring的Assert工具类的用法

简介 今天在看spring mvc源码时看到下面代码,感觉蛮有意思的,在这里记录下 Assert断言工具类,通常用于数据合法性检查,在JAVA编程中,通常会编写如下代码: if (name == null || name.equls("")) {      throw new IllegalArgumentException("参数错误!");  }        在所有方法中都使用手工检测合法性的方式并不是太好,因为这样影响了代码的可读性,若使用Assert工具类

Python——assert、isinstance的用法

1.assert 函数说明: assert语句是一种插入调试断点到程序的一种便捷的方式. 使用范例 assert 3 == 3 assert 1 == True assert (4 == 4) print('-----------') assert (3 == 4) ''' 抛出AssertionError异常,后面程序不执行 ''' print('-----------') 输出结果: D:\Users\lenovo\Anaconda3\python.exe F:/机器学习/生物信息学/Cod

[Python]python中assert和isinstance的用法

assert语句是一种插入调试断点到程序的一种便捷的方式. assert 3 == 3 assert 1 == True assert (4 == 4) print('-----------') assert (3 == 4) ''' 抛出AssertionError异常,后面程序不执行 ''' print('-----------') isinstance函数说明:当我们定义一个class的时候,我们实际上就定义了一种数据类型.我们定义的数据类型和Python自带的数据类型,比如str.lis

python中assert和isinstance的用法

assert语句是一种插入调试断点到程序的一种便捷的方式. assert 3 == 3 assert 1 == True assert (4 == 4) print('-----------') assert (3 == 4) ''' 抛出AssertionError异常,后面程序不执行 ''' print('-----------') isinstance函数说明:当我们定义一个class的时候,我们实际上就定义了一种数据类型.我们定义的数据类型和Python自带的数据类型,比如str.lis

NSAssert详解

NSAssert是foundation.framework中定义的一个宏:#define NSAssert(condition, desc, ...)第一个参数为一个条件判断,如果为假,则抛出异常,显示第二个参数所描述的信息. 例如:NSAssert(2>=3, @"2>=3 is false!");在debug模式下运行,会终止程序,并抛出如下异常:2013-04-24 09:24:16.618 TestAssertion[825:c07] *** Terminating

C和指针之学习笔记(6)

第17章 经典数据结构类型 堆栈 堆栈接口提供三种基本的操作:push.pop 和 top. Push:把一个新值压入到堆栈的顶部. Pop: 只把顶部元素从堆栈中移除,它并不返回这个值. Top: 返回顶部元素的值,但它并不把顶部元素从堆栈中移除.   (1)堆栈接口 #ifndef STACK_H #define STACK_H #include<stdlib.h> #define STACK_TYPE int //push 把一个新值压入到堆栈中,它的参数是需要被压入的值 void pu

c语言中assert的用法

1 /************************************************************************* 2 > File Name: assert.c 3 > Author: Mr.Yang 4 > Purpose:演示函数assert的用法 5 > Created Time: 2017年05月29日 星期一 19时57分54秒 6 **************************************************

Delphi 中ASSERT用法

http://blog.csdn.net/dongyonggan/article/details/5780979 用法:ASSERT(表达式) 如果为假,ASSERT会产生一个EASSERTIONFAiled异常,显示为 Assertion Failed (C:/src/unit1.pas, [size=+0]line 34) 如果不想再使用这些检查时,可以使用($ASSERTIONS OFF)或($C-)编译指令 要想使Assert在整个项目中失效, 关闭Project Options | C

转:assert()函数用法总结

assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h>void assert( int expression ); assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行.请看下面的程序清单badptr.c: #include <stdio.h>#include <a