Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: va_arg... recursion: changing arguments and the using recursion |
| Date | 2012-04-26 16:28 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4F99B006.1000203@verizon.net> (permalink) |
| References | <6923270.398.1335376959698.JavaMail.geo-discussion-forums@vbcg6> <4F998F2F.9000906@verizon.net> <20174875.886.1335469448966.JavaMail.geo-discussion-forums@vbxz8> |
On 04/26/2012 03:44 PM, jononanon@googlemail.com wrote:
> Thanks for those explanations and the code.
>
> On Thursday, April 26, 2012 8:08:47 PM UTC+2, James Kuyper wrote:
>> #include <stdarg.h>
>> #include <stdio.h>
>>
>> void go(int num, ...)
>> {
>> //...
>> int p[num + 1];
>
> Does this work with Microsoft compiler? I seem to remember that the MS compiler complained that it is not a fixed length array (given variable num) and thus didn't work.
> Is this a C90 or c99 feature?
> Wow I just realized that we have C11!
It's a C99 feature, and one supported by virtually every compiler that
makes any attempt at C99 conformance; I believe that this does not
include the M$ compiler.
For C90 code, if there were no upper limit on the value of num, you
would have to do something like this. However, the way your program is
written, the recursive call to go() always has exactly 3 arguments, but
will look for num arguments, so it has undefined behavior if num > 3. An
array with a fixed length of 4 will be adequate for any case where the
behavior is actually defined.
If you actually wanted to be able to handle a variable number of
arguments, the right approach is to define a non-recursive version of
your function which takes a variable number of arguments, and a
recursive subroutine which takes a va_list parameter. The va_list
parameter gives the subroutine access to the parameters of the variadic
function.
> I've just read about TAIL RECURSION, where a stack can be essentially reused, if the last call of a function is the recursive call. Well then tail recursion with reuse of the stack is not possible in portable C, right?
When tail call optimization is possible, sufficiently good compilers can
often be counted on to perform it for you; you don't need to do anything.
Any routine which for which that optimization is possible can, instead,
be reorganized to perform a loop instead of being actually recursive,
which has the same effect. John Reyes has already provided an example,
so I won't bother. Notice that the non-recursive version is actually
simpler than the recursive version - this is not uncommon.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com - 2012-04-25 11:02 -0700
Re: va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com - 2012-04-25 11:16 -0700
Re: va_arg... recursion: changing arguments and the using recursion James Kuyper <jameskuyper@verizon.net> - 2012-04-26 14:08 -0400
Re: va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com - 2012-04-26 12:44 -0700
Re: va_arg... recursion: changing arguments and the using recursion John Reye <jononanon@googlemail.com> - 2012-04-26 13:02 -0700
Re: va_arg... recursion: changing arguments and the using recursion John Reye <jononanon@googlemail.com> - 2012-04-26 13:13 -0700
Re: va_arg... recursion: changing arguments and the using recursion James Kuyper <jameskuyper@verizon.net> - 2012-04-26 16:28 -0400
Re: va_arg... recursion: changing arguments and the using recursion James Kuyper <jameskuyper@verizon.net> - 2012-04-26 16:34 -0400
Re: va_arg... recursion: changing arguments and the using recursion John Reye <jononanon@googlemail.com> - 2012-04-26 13:37 -0700
csiph-web