论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

The Standard C Library for Linux

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:38:13

Part Six: <assert.h> Diagnostics for Programmers
By James M. Rogers

--------------------------------------------------------------------------------

The last article was on <stdlib.h> Standard Library. This article is on <assert.h> Diagnostics for Programmers.

I am assuming a knowledge of c programming on the part of the reader. There is no guarantee of accuracy in any of this information nor suitability for any purpose.

If used properly assertions will allow programmers to much more easily document and debug your code with no impact on runtime performance. Assertions are not meant to be used for production code as they cause the program to terminate with an error condition. Since assertions are never to be used in production code they are not useful in finding runtime errors such as a failure to allocate memory. You must still handle failed return conditions of all function calls the same as always.

Instead, what assertions allow you to do is document the assumptions that you make as you program and allow you to debug the obvious logic errors that you have made. As you program around these logic errors you can modify your assertions to not die on errors that you are now handling.

The example is rogers_example06.c :

#include <stdio.h>
#include <assert.h>

/*

This program is written to demonstrate the <stdlib.h> library.

This program will demonstrate the assert function

Written by James M. Rogers

12 April 1999

Released to the Public Domain on this date.

*/

/*

Compile this with the -DNDEBUG flag in order to _NOT_

compile the assert statement.

So all production boxes should compile with the -DNDEBUG flag.

*/

main (){

int i=1;
int j=0;
assert(j!=0);
printf("%s\n", i/j);
}

In this program I will demonstrate the use of assertions by using a simple program that asks for two numbers and then divides the first number by the second. Compile the program with the following:

gcc -DNDEBUG rogers_example06.c -o assert

and then run ./assert and try to divide by zero. The flag -NDEBUG will cause your assertion to generate no runtime code. This flag should be used in all production environements

Your program will core dump with almost no indication of the problem. Now recompile the program with the following:

gcc rogers_example06.c -o assert

Now run the program again and again try to divide by zero. This time it should be much more apparrent what the problem is and very easy to locate the exact line that had the problem.

As always, if you see an error in my documentation please tell me and I will correct myself in a later document. See corrections at end of the document to review corrections to the previous articles.

Assertions

#include <assert.h>
void assert(int expression);

From:www.linuxgazette.com
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058