The Standard C Library for Linux

金雕国际纹绣晁

金雕国际纹绣晁

2016-02-19 12:40

岁数大了,QQ也不闪了,微信也不响了,电话也不来了,但是图老师依旧坚持为大家推荐最精彩的内容,下面为大家精心准备的The Standard C Library for Linux,希望大家看完后能赶快学习起来。

  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", 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
展开更多 50%)
分享

猜你喜欢

The Standard C Library for Linux

编程语言 网络编程
The Standard C Library for Linux

The Standard C Library for Linux:stdlib.h

编程语言 网络编程
The Standard C Library for Linux:stdlib.h

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

The Standard C Library for Linux:ctype.h

编程语言 网络编程
The Standard C Library for Linux:ctype.h

LINUX C语言开发简介

编程语言 网络编程
LINUX C语言开发简介

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

Linux上搭建C/C++IDE开发环境

Linux Linux命令 Linux安装 Linux编程 Linux桌面 Linux软件 Linux内核 Linux管理
Linux上搭建C/C++IDE开发环境

LINUX C语言开发之2

编程语言 网络编程
LINUX C语言开发之2

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

串的实现实验

串的实现实验

Smarty实例教学 实例篇

Smarty实例教学 实例篇
下拉加载更多内容 ↓