evil@arcturus.UUCP (Wade Guthrie) (10/12/89)
For what purpose are the __LINE__ and __FILE__ macros used? Are these only useful for the writers of code that, in turn, produces compileable code, or is there some use for the applications programmer? Thanks in advance. Wade Guthrie evil@arcturus.UUCP Rockwell International Anaheim, CA (Rockwell doesn't necessarily believe / stand by what I'm saying; how could they when *I* don't even know what I'm talking about???)
howard@aic.dpl.scg.hac.com (Mike Howard (213)317-5690) (10/13/89)
In article <6257@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes: >For what purpose are the __LINE__ and __FILE__ macros used? >... is there some use for the applications programmer? I saw this in some programming article as a debugging usage, though I've never used it: #ifdef DEBUG #define DTRACE(var) fprintf(stderr, "%s:%d, var=%d\n",__FILE__,__LINE__,var) #else #define DTRACE(var) #endif Mike Howard howard@aic.hrl.hac.com
cpcahil@virtech.UUCP (Conor P. Cahill) (10/13/89)
In article <6257@arcturus>, evil@arcturus.UUCP (Wade Guthrie) writes: > For what purpose are the __LINE__ and __FILE__ macros used? Are these > only useful for the writers of code that, in turn, produces compileable > code, or is there some use for the applications programmer? I usually use these for debugging messages: fprintf(stderr,"%s(%d): message\n", __FILE__,__LINE__); and for logfile messages of the same format. Of course, each time you use __FILE__, you get a separate string added to your data space, so I would limit this to development code and strip most of it out of production code (using #ifdefs of course). -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+
henry@utzoo.uucp (Henry Spencer) (10/13/89)
In article <6257@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes: >For what purpose are the __LINE__ and __FILE__ macros used? Are these >only useful for the writers of code that, in turn, produces compileable >code, or is there some use for the applications programmer? Their only major use is for generation of error messages that identify the exact location of the problem. -- A bit of tolerance is worth a | Henry Spencer at U of Toronto Zoology megabyte of flaming. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
gwyn@smoke.BRL.MIL (Doug Gwyn) (10/14/89)
In article <6257@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes: >For what purpose are the __LINE__ and __FILE__ macros used? Are these >only useful for the writers of code that, in turn, produces compileable >code, or is there some use for the applications programmer? We've found them extremely helpful in identifying "memory leaks" (allocations without corresponding deallocations) and other such usage errors in a large project. The way we did this is typified by: #ifdef MmLOG extern pointer Mm_LQAllo( unsigned nbytes, const char *file, int line ); #define Mm_QAllo( nbytes ) Mm_LQAllo( nbytes, __FILE__, __LINE__ ) #else extern pointer Mm_QAllo( unsigned nbytes ); #endif The application uses Mm_QAllo() to allocate a chunk of memory and a similar Mm_QFree() function to deallocate it. When MmLOG is defined during compilation of the application, both these functions actually invoke a different function, Mm_LQAllo() or Mm_LQFree(), passing the application souce code file name and line number as arguments. The implementation of the Mm_L*() functions maintains internal data structures that keep track of all current allocations, and if an attempt is made to deallocate a not-currently-allocated block an error message results, containing the source code information giving the location of the erroneous Mm_QFree() call. Other checks are also made. At the end of application execution, an atexit()-registered function inspects the Mm_L*() data structures and lists all blocks that are considered still allocated. This should give some indication of how __LINE__ and __FILE__ can be useful.
condict@cs.vu.nl (Michael Condict) (10/16/89)
In article <6257@arcturus> evil@arcturus.UUCP (Wade Guthrie) writes: >For what purpose are the __LINE__ and __FILE__ macros used? Are these >only useful for the writers of code that, in turn, produces compileable >code, or is there some use for the applications programmer? My favorite use in application programs is in the definition of a debugging macro that, given a Boolean expression, evaluates it and reports an error message if false. (It usually also does something drastic like intentionally dumping core.) The __LINE__ and __FILE__ vars provide a handy way to report a uniquely identify which assertion failed. Example: #define ASSERT(b) \ if (!b) { fprintf(stderr, "Assertion violated in %s, line %d. Goodbye.\n", __FILE__, __LINE__); abort(); } Michael Condict Vrije University Amsterdam