old-www/LDP/LG/issue41/rogers.html

126 lines
5.7 KiB
HTML

<!--startcut ==========================================================-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.07 [en] (X11; I; Linux 2.0.36 i686) [Netscape]">
<META NAME="Author" CONTENT="James M. Rogers">
<META NAME="Description" CONTENT="This article is the sixth in a series designed to explore the Standard C library implementation available for Linux">
<META NAME="Keywords" CONTENT="linux, standard c library, character functions">
<META NAME="Classification" CONTENT="Second Year Programming">
<TITLE>The Standard C Library for Linux Issue 41</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!--endcut ============================================================-->
<H4>
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<CENTER>
<H1>
<FONT COLOR="#800000">The Standard C Library for Linux</FONT></H1></CENTER>
<CENTER>
<H3>
<FONT COLOR="#000080">Part Six: &lt;assert.h> Diagnostics for Programmers</FONT></H3></CENTER>
<CENTER>
<H4>
By <A HREF="mailto:jrogers@u.washington.edu">James M. Rogers</A></H4></CENTER>
<HR>
<P>The last article was on &lt;stdlib.h> Standard Library.&nbsp; This article
is on &lt;assert.h> Diagnostics for Programmers.
<P>I am assuming a knowledge of c programming on the part of the reader.&nbsp;
There is no guarantee of accuracy in any of this information nor suitability
for any purpose.
<P>If used properly assertions will allow programmers to much more easily
document and debug your code with no impact on runtime performance.&nbsp;
Assertions are not meant to be used for production code as they cause the
program to terminate with an error condition.&nbsp; 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.&nbsp; You must still handle
failed return conditions of all function calls the same as always.
<P>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.&nbsp; As you program around these logic errors you
can modify your assertions to not die on errors that you are now handling.
<P>The example is <A HREF="rogers_example06.c">rogers_example06.c</A>
.&nbsp; 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.&nbsp; Compile the program with the following:
<P>gcc -DNDEBUG rogers_example06.c -o assert
<P>and then run ./assert and try to divide by zero. The flag -NDEBUG will
cause your assertion to generate no runtime code.&nbsp; This flag should
be used in all production environements
<P>Your program will core dump with almost no indication of the problem.&nbsp;
Now recompile the program with the following:
<P>gcc rogers_example06.c -o assert
<P>Now run the program again and again try to divide by zero.&nbsp; This
time it should be much more apparrent what the problem is and very easy
to locate the exact line that had the problem.
<P>As always, if you see an error in my documentation please tell me and
I will correct myself in a later document.&nbsp; See corrections at end
of the document to review corrections to the previous articles.
<P><B><FONT SIZE=+1>Assertions</FONT></B>
<UL><TT>#include &lt;assert.h></TT>
<P><TT>void assert(int expression);</TT></UL>
<P>
<HR>
<P>A correction to Part *:
<BR>&nbsp;
<P>
<HR>
<H4>
Bibilography:</H4>
<I>The ANSI C Programming Language, Second Edition</I>, Brian W. Kernighan,
Dennis M. Ritchie, Printice Hall Software Series, 1988
<P><I>The Standard C Library</I>, P. J. Plauger, Printice Hall P T R, 1992
<P><I>The Standard C Library, Parts 1, 2, and 3</I>, Chuck Allison, <I>C/C++
Users Journal</I>, January, February, March 1995
<P>STDIO(3), BSD MANPAGE, <I>Linux Programmer's Manual</I>, 29 November
1993
<P> <HR><P>
&nbsp;<BR>
<CENTER>
<H4>
Previous "The Standard C Library for Linux" Articles</H4></CENTER>
<A HREF="http://www.linuxgazette.com/issue24/rogers.html"><I>The Standard
C Library for Linux, stdio.h</I>, James M. Rogers, January 1998</A>
<BR><A HREF="http://www.linuxgazette.com/issue31/rogers1.html"><I>The Standard
C Library for Linux, stdio.h</I>, James M. Rogers, July 1998</A>
<BR><A HREF="http://www.linuxgazette.com/issue32/rogers.html"><I>The Standard
C Library for Linux, stdio.h</I>, James M. Rogers, August 1998</A>
<BR><A HREF="http://www.linuxgazette.com/issue38/rogers.html"><I>The Standard
C Library for Linux, ctype.h</I>, James M. Rogers, March 1999</A>
<BR><A HREF="http://www.linuxgazette.com/issue39/rogers.html"><I>The Standard
C Library for Linux, stdlib.h</I>, James M. Rogers, April 1999</A>
<!--===================================================================-->
<P> <hr> <P>
<center><H5>Copyright &copy; 1999, James M. Rogers <BR>
Published in Issue 41 of <i>Linux Gazette</i>, May 1999</H5></center>
<!--===================================================================-->
<P> <hr> <P>
<A HREF="./index.html"><IMG ALIGN=BOTTOM SRC="../gx/indexnew.gif"
ALT="[ TABLE OF CONTENTS ]"></A>
<A HREF="../index.html"><IMG ALIGN=BOTTOM SRC="../gx/homenew.gif"
ALT="[ FRONT PAGE ]"></A>
<A HREF="./weaver.html"><IMG SRC="../gx/back2.gif"
ALT=" Back "></A>
<A HREF="./nielsen2.html"><IMG SRC="../gx/fwd.gif" ALT=" Next "></A>
<P> <hr> <P>
<!--startcut ==========================================================-->
</BODY>
</HTML>
<!--endcut ============================================================-->