国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

The Standard C Library for Linux

2019-11-17 05:37:53
字體:
來源:轉載
供稿:網友

  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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗江县| 霍林郭勒市| 关岭| 金门县| 达尔| 厦门市| 闸北区| 遵化市| 尤溪县| 汝城县| 葵青区| 沙田区| 自治县| 东海县| 屯留县| 长海县| 顺义区| 子长县| 资中县| 汤阴县| 禹州市| 嵊泗县| 台南县| 遂川县| 廊坊市| 安丘市| 娄底市| 民丰县| 化隆| 勃利县| 高唐县| 四会市| 咸宁市| 阆中市| 永修县| 遵化市| 玉环县| 绍兴县| 西宁市| 额尔古纳市| 泰安市|