home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20083 < prev    next >
Encoding:
Text File  |  1993-01-23  |  1.8 KB  |  53 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Unwanted float to double conversion in variable length arg lists?
  5. Message-ID: <1993Jan22.193816.13266@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <C18924.2yp@inews.Intel.COM>
  8. Date: Fri, 22 Jan 1993 19:38:16 GMT
  9. Lines: 42
  10.  
  11. dmarer@td2cad.intel.com (Dennis Marer) writes:
  12.  
  13. >I've got a curious problem when passing floats in a variable length argument
  14. >list...they are automatically converted to doubles, EVEN when I typecast.
  15. >Consider:
  16.  
  17. >#include <stdarg.h>
  18.  
  19. >void func(int num,...) {
  20. >    va_list va;
  21. >    float x,y;
  22.  
  23. >    va_start(va,num);
  24.      ...
  25. >    x = va_arg(va,float);
  26.      ...
  27.  
  28.  
  29. The results are undefined if you attempt to use types which undergo
  30. promotion with va_arg.   (ANSI Standard section 4.8.1.2)
  31.  
  32. When you pass a float value (for example) to a function without a
  33. prototype in scope or as a parameter corresponding to the "...",
  34. the compiler *must* promote it to a double.  This typically has a
  35. different representation than a float, as well as a different size.
  36. If you attempt to retrieve a float from the argument list, you will
  37. get garbage, and likely wrong stack offsets for the remainder of the
  38. arguments.  You must retrieve only the promoted type.
  39.  
  40. If floats and doubles happened to have the same size and representation
  41. on a given system (not impossible), the code would work.
  42.  
  43. Alternatively, an implementation might provide pragmas which allow
  44. floats to be passed and retrieved.  I don't know of such an
  45. implementation, and the code would not be portable anyway.
  46.  
  47. The same applies to short and char types, which are promoted to int
  48. when passed.  You must retrieve them as ints, not as chars or shorts,
  49. to get predictable results.
  50. -- 
  51.  
  52. Steve Clamage, TauMetric Corp, steve@taumet.com
  53.