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


Groups > comp.lang.c > #12794

Re: variadic function

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: variadic function
Date 2011-11-06 01:31 +0000
Organization A noiseless patient Spider
Message-ID <20111105181850.993@kylheku.com> (permalink)
References <j94dhl$6ua$1@speranza.aioe.org>

Show all headers | View raw


On 2011-11-05, Quentin Pope <qp19433@hotmail.NOSPAM.com> wrote:
> Consider the variadic function with the following prototype:
> int foo(int num,...);
> Here 'num' specifies the number of arguments, and assume that all the
> arguments that should be passed to this function are of type int.
> (My question has nothing to do with the definition of the function
> foo, so don't bother about it.)
>
> If I call the function as:
> foo(2,3,4,5,6,7,8);/*More arguments than expected*/
>
> Here I call foo with too many arguments than expected by its
> definition.

There is no way that a definition can express that, though.  A variadic
function does not expect any particular number of parameters (just at least the
required ones).

A variadic funtion can fail to extract all the parameters by calling the va_arg
macro fewer times than there are parameters.

> insufficient arguments for the format, the behavior is undefined. If
> the format is exhausted while arguments remain, the excess arguments
> are evaluated (as always) but are otherwise ignored.

Exactly.

> This is highly specific to fprintf. I wonder if it applies to all
> variadic functions.

Nothing is written that all arguments must be consumed.

> Can I assume that the behavior of this function call is UB by
> ommission?

No, because there is no omission. Every step can be verified to have
a defined behavior.

"UB by omission" occurs in cases when you actually cannot infer 
what the behavior should be.

The concern about what happens to arguments that are not pulled out
is a concern about some steps that the program /did not/ execute.

Look at the steps that the program /did/ execute, and are those defined?

Sometimes a program can invoke UB by neglecting to take some required steps
that a situation calls for. That kind of situation has to be spelled out.

For instance, if an input operation is done on an update stream after an output
operation, without an intervening fflush, the behavior is undefined.  If this
was not spelled out, the behavior would have to be considered defined (since an
update stream is one on which you can do a mixture of reads and writes, and if
no combination is documented as broken, then you can assume it's not broken.)

> This email and any file attachments(s) are confidential, may be legally
> privileged, and are intended solely for use by the identified recipient

The above footer is egregiously excessive and netiquette-violating. I
understand it's not your fault. If you give me the e-mail address of whoever is
responsible I will share with them an outsider's perspective.  You should not
have your posting stigmatized by reams of legalese garbage.

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