Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: "P.J. Plauger" <pjp@xxxxxxxxxxxxxx>
- Date: Sun, 16 Jul 2006 10:27:22 -0400
"David R Tribble" <david@xxxxxxxxxxx> wrote in message
news:1153004809.505381.156810@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Wojtek Lerch wrote:
Instead, POSIX/XSI chose to require that all arguments from 1$ to N$
must
be specified in the format (and don't have to be the same type), and
that
you can't mix numbered specifications with unnumbered ones in the same
call (which makes advancing the pointer a non-issue when numbered
specifications are used). Do you think that's still underspecified?
P.J. Plauger wrote:
I should have clarified that my web search turned up three (count 'em 3)
different descriptions of the N$ mechanism. Two described themselves as
official and were mutually contradictory. The third was simply vague.
Since the customer wanted our library to "do what everyone else does"
I concluded that none of these specs was a safe guide to actual practice.
Admittedly, the Open Group spec might be self consistent. (The reason
for the "might be" appears below.) It simply requires printf to scan the
format string N times to obtain the Nth argument, or to stack up all N
arguments in one pass by some unspecified but cumbrous mechanism. That's
the only way to safely skip over a heterogeneous sequence of arguments.
The "cumbrous" method I used was a single scan that kept a stack
of descriptors for each $n specifier found in a left-to-right scan of
the format string. Each descriptor contained the format specifier
type (e.g., 'd', 's', 'x', etc.). This provides enough information to
extract the arguments from the call stack. This approach also
allows you to verify that all format specifiers $1 through $n are
actually present in the format string.
Our printf family has 29 different calls to va_arg, each effectively
fetching a different type of argument. By the time you duplicate the
logic for determining which of those 29 argument types you want to
fetch, and the 29-way switch for getting the argument, it starts
looking cumbrous to me. YMMV.
The only trick is preallocating enough $n descriptors for the
worst-case format string. If all you need for each descriptor is
the % datatype, they can be implemented as a character array,
perhaps 80 or so chars in length (where it's assumed that 80
should be enough for most users). Or more elaborate dynamic
allocations can be used to handle an unlimited number of $n
specifiers.
Any library function that requires an open-ended amount of storage,
or a guess at a safe upper bound, starts looking cumbrous to me.
Admittedly, five bits per argument ain't all that bad, but the
code duplication I cited above is at best graceless and bloated,
at worst a maintenance nightmare.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
.
- Follow-Ups:
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: Bjorn Reese
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- References:
- Why doesn't the standard allow reuse of arguments in printf() ?
- From: spibou
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: P.J. Plauger
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: Douglas A. Gwyn
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: P.J. Plauger
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: Wojtek Lerch
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: P.J. Plauger
- Re: Why doesn't the standard allow reuse of arguments in printf() ?
- From: David R Tribble
- Why doesn't the standard allow reuse of arguments in printf() ?
- Prev by Date: Re: Why doesn't the standard allow reuse of arguments in printf() ?
- Next by Date: Re: Why doesn't the standard allow reuse of arguments in printf() ?
- Previous by thread: Re: Why doesn't the standard allow reuse of arguments in printf() ?
- Next by thread: Re: Why doesn't the standard allow reuse of arguments in printf() ?
- Index(es):
Relevant Pages
|