home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / std / c / 2361 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  1.9 KB

  1. Path: sparky!uunet!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.std.c
  4. Subject: Re: can I call vprintf() twice in a row?
  5. Date: 25 Jul 1992 06:13:45 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 45
  8. Message-ID: <24888@dog.ee.lbl.gov>
  9. References: <ZJ2FoB1w165w@tsoft.sf-bay.org>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <ZJ2FoB1w165w@tsoft.sf-bay.org> bbs.dennis@tsoft.sf-bay.org
  14. (Dennis Yelle) writes:
  15. >    Is the following legal ansi c?
  16. >    I beleave that it is, but if not, I would like to know why not.
  17.  
  18. >#include <stdarg.h>
  19. >#include <stdio.h>
  20. >
  21. >void report( char *fmt, ...)
  22. >{
  23. >    va_list arg_ptr;
  24. >    
  25. >    va_start( arg_ptr, fmt);
  26. >    vprintf( fmt, arg_ptr);
  27. >    vprintf( fmt, arg_ptr);  /* Yes, we want to print it twice.     */
  28. >                             /* In the original program, the second */
  29. >                             /* was vfprintf to a log file.         */
  30. >    va_end( arg_ptr);
  31. >}
  32.  
  33. X3.159-1989, section 4.8, p. 123, ll. 13--15:
  34.  
  35.     The object |ap| [equivalently, |arg_ptr| above] may be passed as
  36.     an argument to another function; if that function invokes the |va_arg|
  37.     macro with parameter |ap|, the value of |ap| in the calling function
  38.     is indeterminate and shall be passed to the |va_end| macro prior to
  39.     any further reference to |ap|.
  40.  
  41. Since |vprintf| certainly uses |va_arg| on |arg_ptr|, the code quoted
  42. above violates this `shall' (which, note, is outside a `constraints'
  43. section, so that this violation does not require a diagnostic).
  44.  
  45. There is a trivial fix for the report() function:
  46.  
  47.     va_start(arg_ptr, fmt);
  48.     vprintf(fmt, arg_ptr);
  49.     va_end(arg_ptr);
  50.     va_start(arg_ptr, fmt);
  51.     vprintf(fmt, arg_ptr);
  52.     va_end(arg_ptr);
  53.  
  54. will do the trick.
  55. -- 
  56. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  57. Berkeley, CA        Domain:    torek@ee.lbl.gov
  58.