交错变量参数处理 [英] Interleaved variable argument processing
问题描述
我最近被问到第二次开始处理变量
参数列表是否合法,/之前/你已经完成第一次
时间。 (我不知道为什么提问者可能想要这样做,我很害怕。
因为他通常相当聪明,我认为他有充分的理由来
问。)
以下代码说明了这一点,我认为(但不要将它用作
va_arg教程!它有点假设在名为arg的
之后至少有六个args):
#include< stdio.h>
#include< stdarg.h>
int foo(int i,...)
{
va_list ap = {0};
va_list aq = {0};
int sum = i;
va_start(ap,i );
sum + = va_arg(ap,int);
sum + = va_arg(ap,int);
va_start(aq, i);
sum + = va_arg(ap,int);
sum + = va_arg(ap,int);
sum + = va_arg(aq,int);
sum + = va_arg(aq,int);
sum + = va_arg(ap,int);
sum + = va_arg(aq,int);
sum + = va_arg(ap,int);
sum + = va_a rg(aq,int);
va_end(ap);
sum + = va_arg(aq,int);
sum + = va_arg (aq,int);
va_end(aq);
返还金额;
}
int main(void)
{
printf("%d \ n",foo(6,5,4,3,2,1,0) );
返回0;
}
通读了C99的相关部分,我发现没有
基于标准的原因为什么这种交错应该不起作用(事实上,
它对我测试代码的单个编译器起作用)。
我是否错过了什么,或者我认为这是一种合法的,
定义明确的技术?
-
Richard Heathfield: bi****@eton.powernet.co.uk >
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
有这个要注意:
[草案99]
I7.15.1.4
条款3.
" ...不应再为相同的ap [va_list]调用va_start类型]
没有对同一个ap进行干扰调用va_end。
鉴于常见的编译器实现,它的工作原理并不奇怪/>
虽然。
Jason写道:
有这个要注意:
[草案99]
I7.15.1.4
第3条。
" ... ... va_start将不要再为同一个ap [va_list类型]调用
而没有为同一个ap调用va_end。
鉴于常见的编译器实现,它的工作原理并不奇怪
虽然。
这是一个不同的ap。
-
Richard Heathfield: bi****@eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
" Ivan Vecerina" < ivecATmyrealboxDOTcom>写道:
< snip>
这是我对你的样品代码的唯一建议:而不是两次调用va_start,为什么不使用va_copy?
也许是因为他没有C99编译器。 :-)
-
Richard Heathfield: biwel@eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
I was recently asked whether it is legal to start processing a variable
argument list a second time, /before/ you''ve finished with it the first
time. (I have no idea why the questioner might want to do this, I''m afraid.
Since he''s normally fairly bright, I presume he had a good reason for
asking.)
The following code illustrates the point, I think (but don''t use it as a
va_arg tutorial!, as it kinda assumes there are at least six args after the
named arg):
#include <stdio.h>
#include <stdarg.h>
int foo(int i, ...)
{
va_list ap = {0};
va_list aq = {0};
int sum = i;
va_start(ap, i);
sum += va_arg(ap, int);
sum += va_arg(ap, int);
va_start(aq, i);
sum += va_arg(ap, int);
sum += va_arg(ap, int);
sum += va_arg(aq, int);
sum += va_arg(aq, int);
sum += va_arg(ap, int);
sum += va_arg(aq, int);
sum += va_arg(ap, int);
sum += va_arg(aq, int);
va_end(ap);
sum += va_arg(aq, int);
sum += va_arg(aq, int);
va_end(aq);
return sum;
}
int main(void)
{
printf("%d\n", foo(6, 5, 4, 3, 2, 1, 0));
return 0;
}
Having read through the relevant section of C99, I could find no
standard-based reason why this interleaving should not work (and, indeed,
it does work on the single compiler I tested the code with).
Did I miss something, or am I right in thinking this is a legal,
well-defined technique?
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
There is this to note:
[draft 99]
I7.15.1.4
clause 3.
"...va_start shall not be invoked again for the same ap [ va_list type ]
without an intervening invocation of va_end for the same ap."
Given common compiler implementations it''s not surprising that it works
though.
Jason wrote:
There is this to note:
[draft 99]
I7.15.1.4
clause 3.
"...va_start shall not be invoked again for the same ap [ va_list type ]
without an intervening invocation of va_end for the same ap."
Given common compiler implementations it''s not surprising that it works
though.
It''s a different ap.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
"Ivan Vecerina" <ivecATmyrealboxDOTcom> wrote:
<snip>
And that''s the only suggestion I would have regarding your sample
code: instead of calling va_start twice, why not use va_copy ?
Perhaps because he doesn''t have a C99 compiler. :-)
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
这篇关于交错变量参数处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!