home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Unwanted float to double conversion in variable length arg lists?
- Message-ID: <1993Jan22.193816.13266@taumet.com>
- Organization: TauMetric Corporation
- References: <C18924.2yp@inews.Intel.COM>
- Date: Fri, 22 Jan 1993 19:38:16 GMT
- Lines: 42
-
- dmarer@td2cad.intel.com (Dennis Marer) writes:
-
- >I've got a curious problem when passing floats in a variable length argument
- >list...they are automatically converted to doubles, EVEN when I typecast.
- >Consider:
-
- >#include <stdarg.h>
-
- >void func(int num,...) {
- > va_list va;
- > float x,y;
-
- > va_start(va,num);
- ...
- > x = va_arg(va,float);
- ...
-
-
- The results are undefined if you attempt to use types which undergo
- promotion with va_arg. (ANSI Standard section 4.8.1.2)
-
- When you pass a float value (for example) to a function without a
- prototype in scope or as a parameter corresponding to the "...",
- the compiler *must* promote it to a double. This typically has a
- different representation than a float, as well as a different size.
- If you attempt to retrieve a float from the argument list, you will
- get garbage, and likely wrong stack offsets for the remainder of the
- arguments. You must retrieve only the promoted type.
-
- If floats and doubles happened to have the same size and representation
- on a given system (not impossible), the code would work.
-
- Alternatively, an implementation might provide pragmas which allow
- floats to be passed and retrieved. I don't know of such an
- implementation, and the code would not be portable anyway.
-
- The same applies to short and char types, which are promoted to int
- when passed. You must retrieve them as ints, not as chars or shorts,
- to get predictable results.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
-