assert的用处
ASSERT函数是用于调试中,也就是说在你的代码中当是Debug的时候
它完成对参数的判断,如果是TRUE则什么都不做,如果是FALSE则
弹出一个程序中断对话框提示程序出现错误。
在Release版本中它是什么作用都不起。
它主要是监视程序在调试运行的过程中的运行情况,
多多使用它,绝对有好处,没有一点坏处。
例如:
/* ASSERT.C: In this program, the analyze_string function uses
* the assert function to test several conditions related to
* string and length. If any of the conditions fails, the program
* prints a message indicating what caused the failure.
*/ #include <stdio.h>
#include <assert.h>
#include <string.h> void analyze_string( char *string ); /* Prototype */ void main( void )
{
char test1[] = "abc", *test2 = NULL, test3[] = ""; printf ( "Analyzing string '%s'\n", test1 );
analyze_string( test1 );
printf ( "Analyzing string '%s'\n", test2 );
analyze_string( test2 );
printf ( "Analyzing string '%s'\n", test3 );
analyze_string( test3 );
} /* Tests a string to see if it is NULL, */
/* empty, or longer than 0 characters */
void analyze_string( char * string )
{
assert( string != NULL ); /* Cannot be NULL */
assert( *string != '\0' ); /* Cannot be empty */
assert( strlen( string ) > 2 ); /* Length must exceed 2 */
} Output
Analyzing string 'abc'
Analyzing string '(null)'
Assertion failed: string != NULL, file assert.c, line 24 abnormal program termination
assert的用处的更多相关文章
- python assert的用处
python assert 句语格式及用法很简单.通常程序在运行完之后抛出异常,使用assert可以在出现有异常的代码处直接终止运行. 而不用等到程序执行完毕之后抛出异常. python assert ...
- 断言(assert)和程序的安全保证
断言,用来DEBUG错误的,在DEBUG时发现然后跟踪错误! 通常 写一个程序给别人使用的,这个代码在安全性上的要求是什么呢?直觉上,我们都知道程序不应该崩.但是通常C/C++的程序如果把包含API的 ...
- C++ assert 断言使用
在研究corepattern需要让程序出core, 因此用到的assert, 记录一下. 写严谨代码时,也可以使用assert进行严格的条件判断. 函数原型: #include <asser ...
- UNREFERENCED_PARAMETER的用处
UNREFERENCED_PARAMETER的用处 作用:告诉编译器,已经使用了该变量,不必检测警告! 在VC编译器下,如果您用最高级别进行编译,编译器就会很苛刻地指出您的非常细小的警告.当你生命了一 ...
- iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处
NSAssert和assert是断言,主要的差别是assert在断言失败的时候只是简单的终止程序,而NSAssert会报告出错误信息并且打印出来.所以尽管的使用NSAssert,可以不去使用asser ...
- 【repost】document.write的用处
document.write的用处 document.write是JavaScript中对document.open所开启的文档流(document stream操作的API方法,它能够直接在文档流中 ...
- 【转】代码中特殊的注释技术——TODO、FIXME和XXX的用处
(转自:http://blog.csdn.net/reille/article/details/7161942) 作者:reille 本博客网址:http://blog.csdn.net/reille ...
- C 标准库系列之assert.h
先简单介绍一下<assert.h>头文件,该头文件的目的便是提供一个宏assert的定义,即可以在程序必要的地方使用其进行断言处理:断言在程序中的作用是当在调试模式下时,若程序给出的前提条 ...
- java分享第十四天(TestNG Assert详解)
TestNG Assert 详解org.testng.Assert 用来校验接口测试的结果,那么它提供哪些方法呢? 中心为Assert测试类,一级节点为方法例如assertEquals,二级结点为参 ...
随机推荐
- Mybatis 和 Spring配置
一.使用的jar包就不详细讲解了,下载了Mybatis 和 Spring 的jar包基本上都添加上去了. 一图概括:(这是我使用的ar包,有些不是Mybatis 和 Spring 的 ) 二. web ...
- 安装Docker和下载images镜像和常用Docker命令
我的是centos7,也会6的方法: $sudo yum install docker 直接yum安装contos7使用centos6.5先获取epel源并 启动Docker,并注册开机服务 [roo ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- Android开发之获取相册照片和获取拍照照片
在Android的开发过程中,我们可能会读取手机里面的照片或者通过相机拍摄获取照片,这是两种常用的获取图片的方式,在做项目过程中也会经常遇到,下面来介绍一下这两种获取方式.. 1.从本地相册获取照片: ...
- 2016年6月25日 星期六 --出埃及记 Exodus 14:22
2016年6月25日 星期六 --出埃及记 Exodus 14:22 and the Israelites went through the sea on dry ground, with a wal ...
- CSS3那些不为人知的高级属性
尽管现代浏览器已经支持了众多的CSS3属性,但是大部分设计师和开发人员貌似依然在关注于一些很“主流”的属性,如border-radius.box-shadow或者transform等.它们有良好的文档 ...
- percona-toolkit工具包的使用教程之开发类工具
percona-toolkit工具包的使用教程之开发类工具 1. pt-duplicate-key-checker l 功能介绍: 功能为从mysql表中找出重复的索引和外键,这个工具会将重复的索 ...
- SqlSever基础 intersect 交集 两个查询结果共有的
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 命令行运行R语言脚本(代码)
1 Windows: 键入 cd C:\Program Files\R\R-3.2.0\bin 工作目录切换到R的核心程序目录 键入 R BATCH F:\Test.R 或 Rscript F:\ ...
- CoreLocation
导入框架(Xcode5.0之后可以省略)