Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16609

Re: variadic function

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: variadic function
Date 2012-02-01 11:41 -0800
Organization None to speak of
Message-ID <ln8vkmuysm.fsf@nuthaus.mib.org> (permalink)
References (3 earlier) <kfnty3k9rdd.fsf@x-alumni2.alumni.caltech.edu> <lnmx9c59x8.fsf@nuthaus.mib.org> <kfnwr87z40v.fsf@x-alumni2.alumni.caltech.edu> <ln39avw19d.fsf@nuthaus.mib.org> <slrnjiip8q.s1c.willem@toad.stack.nl>

Show all headers | View raw


Willem <willem@toad.stack.nl> writes:
> Keith Thompson wrote:
> ) Do you suggest that there are *realistic* cases where a call to a
> ) variadic function passes N arguments, and the function itself processes
> ) fewer than N arguments, and where this does not indicate a logical error
> ) in the program?
>
> In SQL, there is a function calles 'coalesce' which returns its first
> non-NULL argument.  It doesn't need to process the remaining arguments.
>
> ) I can imagine an implementation where a variadic function that
> ) fails to consume all its arguments might cause Bad Things To Happen.
> ) For example, each invocation of va_arg() might pop the corresponding
> ) argument off the stack; failing to do so might leave the stack in an
> ) inconsistent state.  It could have much the same effect as passing
> ) two arguments to a non-variadic function with one parameter (say,
> ) by casting a function pointer to an inappropriate type), which has
> ) undefined behavior in C as it's currently defined.
>
> Are you sure about that?
> I thought that was allowed and the second argument was simply ignored.

You're right.  The context of the quoted paragraph was a hypothetical
version of C that doesn't permit invoking va_end() before invoking
va_arg() for all actual arguments.  An implementation as I described
could implement such a hypothetical C, and implementing it *might* be
more convenient than implementing something that handles C as it's
actually defined.  It would not be a conforming implementation of C as
it's actually defined.

This all started with an idle speculation of mine several months ago
about whether, if C had *originally* required all variadic arguments to
be processed, it would create any great inconvenience.  At the time, I
didn't have any examples where it would be a problem.  Several such
examples have now been offered.

The C89 committee *could* have imposed such a requirements, and if they
had, programmers would have managed, but there are some valid techniques
that couldn't be used.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

variadic function Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-11-05 22:32 +0000
  Re: variadic function Keith Thompson <kst-u@mib.org> - 2011-11-05 15:50 -0700
    Re: variadic function Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-11-07 17:41 +0000
  Re: variadic function James Kuyper <jameskuyper@verizon.net> - 2011-11-05 18:53 -0400
    Re: variadic function Keith Thompson <kst-u@mib.org> - 2011-11-05 16:35 -0700
      Re: variadic function Kaz Kylheku <kaz@kylheku.com> - 2011-11-06 01:36 +0000
  Re: variadic function Kaz Kylheku <kaz@kylheku.com> - 2011-11-06 01:31 +0000
  Re: variadic function Barry Schwarz <schwarzb@dqel.com> - 2011-11-06 09:45 -0800
    Re: variadic function Keith Thompson <kst-u@mib.org> - 2011-11-06 11:38 -0800
      Re: variadic function Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 23:24 -0800
        Re: variadic function Keith Thompson <kst-u@mib.org> - 2012-01-25 02:54 -0800
          Re: variadic function Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 18:22 -0800
            Re: variadic function Keith Thompson <kst-u@mib.org> - 2012-01-31 21:50 -0800
              Re: variadic function James Kuyper <jameskuyper@verizon.net> - 2012-02-01 07:55 -0500
              Re: variadic function Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-01 08:06 -0500
                Re: variadic function Keith Thompson <kst-u@mib.org> - 2012-02-01 11:42 -0800
              Re: variadic function Willem <willem@toad.stack.nl> - 2012-02-01 16:15 +0000
                Re: variadic function Keith Thompson <kst-u@mib.org> - 2012-02-01 11:41 -0800
              Re: variadic function Shao Miller <sha0.miller@gmail.com> - 2012-02-01 13:41 -0500
                Re: variadic function blp@cs.stanford.edu (Ben Pfaff) - 2012-02-01 10:45 -0800
              Re: variadic function Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 10:30 -0800
                Re: variadic function Keith Thompson <kst-u@mib.org> - 2012-03-08 13:57 -0800
                Re: variadic function Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-09 13:10 -0800
                Re: variadic function jgk@panix.com (Joe keane) - 2012-03-11 07:56 +0000
                Re: variadic function Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-11 13:01 -0700
                Re: variadic function jgk@panix.com (Joe keane) - 2012-03-12 19:04 +0000
                Re: variadic function Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-13 15:05 -0700

csiph-web