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


Groups > comp.lang.c > #16522

Re: va_args Conformance

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: va_args Conformance
Date 2012-01-31 13:45 -0500
Organization A noiseless patient Spider
Message-ID <jg9csa$cca$1@dont-email.me> (permalink)
References <jg8ssi$5ea$1@dont-email.me> <93f0c4d6-7c90-492d-aced-062800e8e27f@y10g2000vbn.googlegroups.com>

Show all headers | View raw


On 1/31/2012 12:46, Harald van Dijk wrote:
> On Jan 31, 3:12 pm, Shao Miller<sha0.mil...@gmail.com>  wrote:
>> If the following program strictly conforming?
>>
>>     #include<stddef.h>
>>     #include<stdarg.h>
>>
>>     static void test(int, ...);
>>
>>     int main(void) {
>>         test(0, 42);
>>
>>         return 0;
>>       }
>>
>>     static void test(int x, ...) {
>>         typedef unsigned char one_char[1];
>>         /* Valid declaration on the next line */
>>         one_char * unused;
>>         va_list varargs;
>>         unsigned char * byte;
>>
>>         va_start(varargs, x);
>>         if (x)
>>             byte = va_arg(varargs, one_char);
>
> In C90, this is invalid. As your compilers' error messages state, the
> standard does not require va_arg's result to be an lvalue, and a non-
> lvalue array cannot be used in any way except to discard it, to take
> its size, or to uselessly pass it to another variadic function. In
> particular, it cannot be converted to a pointer, and its members
> cannot be accessed.
>

Aha!  Thanks very much.  Indeed, the result is a "value" rather than 
designating an "object."

That such a value can be passed to another function is interesting, 
though...  I suppose it could be passed to a non-variadic function whose 
parameters aren't specified, too.

> In C99 (and presumably C11), this use of va_arg unconditionally
> invokes undefined behaviour: there is no longer any way to pass an
> array to a variadic function without it getting converted to a pointer
> to its first element.

Out of curiosity, how could you pass an array pre-C99?  Or do you mean 
for the strict case where the expression is an array "value" without 
being an lvalue, that you described above?

The program doesn't attempt to pass an array to the 'test' function, by 
the way.  So I think by "this use," you were referring to a construct 
which didn't appear in the program, but which might be associated with 
it.  Or did you really mean a construct in the program?

> A line that unconditionally invokes undefined
> behaviour at runtime is technically allowed in a strictly conforming
> program if the line is never executed, but this would not be the first
> example where compilers divert from the standard in that regard.
>

I see.  Well thanks again!

-- 
"The stationery store has moved.  Aaargh!"

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


Thread

va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 09:12 -0500
  Re: va_args Conformance Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 14:50 +0000
    Re: va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 10:11 -0500
      Re: va_args Conformance Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 17:19 +0000
        Re: va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 12:31 -0500
          Re: va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 12:34 -0500
            Re: va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 12:40 -0500
          Re: va_args Conformance Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 17:52 +0000
    Re: va_args Conformance James Kuyper <jameskuyper@verizon.net> - 2012-01-31 11:27 -0500
      Re: va_args Conformance Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-31 16:53 +0000
        Re: va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 12:09 -0500
        Re: va_args Conformance James Kuyper <jameskuyper@verizon.net> - 2012-01-31 12:15 -0500
  Re: va_args Conformance Harald van Dijk <haraldvdijk@gmail.com> - 2012-01-31 09:46 -0800
    Re: va_args Conformance Shao Miller <sha0.miller@gmail.com> - 2012-01-31 13:45 -0500
      Re: va_args Conformance Harald van Dijk <haraldvdijk@gmail.com> - 2012-01-31 11:28 -0800
      Re: va_args Conformance Harald van Dijk <haraldvdijk@gmail.com> - 2012-01-31 11:33 -0800

csiph-web