Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!brunix!phg From: phg@cs.brown.edu (Peter H. Golde) Newsgroups: comp.software-eng Subject: Re: assert(): a "tutorial" Keywords: assert() Message-ID: <36218@brunix.UUCP> Date: 12 Apr 90 22:59:20 GMT References: <48461@lanl.gov> Sender: news@brunix.UUCP Reply-To: phg@cs.brown.edu (Peter H. Golde) Organization: Brown University Department of Computer Science Lines: 32 In article <48461@lanl.gov> u096000@lanl.gov (Roger A. Cole) writes: >With these goals in mind, I have settled on the following form for assert(), >which I'm using with the Sun3 C compiler. There is a xxx.h part and a >xxx.c part. > >#ifdef DEBUG ># define assert(expr) \ > ((void)((expr) || assertFail(__FILE__, __LINE__))) >#else ># define assert(expr) >#endif Many C compilers will not put identical string literals into the same piece of storage. Using this method results in many copies of the file name in your data space. You by be more efficient by using: #ifdef DEBUG static char _CurrentFileName = __FILE__; #define assert(expr) \ ((void)((expr) || assertFail(_CurrentFileName, __LINE__))) #else #define assert(expr) #endif When doing this, you have to guard against including the .h file multiple times. --Peter Golde (phg@cs.brown.edu)