【发布时间】:2015-06-23 21:21:52
【问题描述】:
我想设计一个带有可变数量参数的函数,其中一个参数本身就是va_list;但是我的代码出了点问题,我不明白是什么......
警告——我的问题不是设计代码做我想做的事情(我找到了绕过问题的方法),而只是了解我做错了什么......
为了解释我的问题,让我们从一个简单的例子开始:即一个函数ffprintf,它的作用类似于fprintf,但将其内容写入多个字符串,其编号由@987654324的第一个参数表示@,其身份由下一个参数给出(这些参数的数量可能因一次调用而异,因此您必须使用可变参数列表)。这样的函数可以这样使用:
FILE *stream0, *stream1, *stream2;
int a, b;
ffprintf (3, stream0, stream1, stream2, "%d divided by %d worths %f", a, b, (double)a / b);
它的代码是:
void ffprintf (int z, ...)
{va_list vlist, auxvlist;
FILE **streams = malloc (z * sizeof(FILE *));
va_start (vlist, z);
for (int i = 0; i < z; ++i)
{streams[i] = va_arg (vlist, FILE *); // Getting the next stream argument
}
char const *format = va_arg (vlist, char const *); // Getting the format argument
for (int i = 0; i < z; ++i)
{va_copy (auxvlist, vlist); // You have to work on a copy "auxvlist" of "vlist", for otherwise "vlist" would be altered by the next line
vfprintf (streams[i], format, auxvlist);
va_end (auxvlist);
}
va_end (vlist);
free (streams);
}
效果很好。现在,还有标准函数vfprintf,其原型是vfprintf (FILE *stream, char const* format, va_list vlist);,您可以像这样使用它来创建另一个具有可变参数列表的函数:
void fprintf_variant (FILE *stream, char const* format, ...)
{
va_list vlist;
va_start (vlist, format);
vfprintf (stream, format, vlist);
va_end (vlist);
}
这也很好用。现在,我的目标是将这两种想法结合起来创建一个函数,我将其命名为vffprintf,您可以像这样使用它:
FILE *stream0, *stream1, *stream2;
void fprintf_onto_streams012 (char const *format, ...)
{va_list vlist;
va_start (vlist, format);
vffprintf (3, stream0, stream1, stream2, format, vlist);
va_end (vlist);
}
我设计了如下代码:
void vffprintf (int z, ...)
{va_list vlist, auxvlist, auxauxvlist;
va_start (vlist, z);
FILE **streams = malloc (z * sizeof(FILE *));
for (int i = 0; i < z; ++i)
{streams[i] = va_arg (vlist, FILE *);
}
char const *format = va_arg (vlist, char const *);
va_copy (auxvlist, va_arg (vlist, va_list)); // Here I get the next argument of "vlist", knowing that this argument is of "va_list" type
for (int i = 0; i < z; ++i)
{va_copy (auxauxvlist, auxvlist);
vfprintf (streams[i], format, auxvlist);
va_end (auxauxvlist);
}
va_end (auxvlist);
va_end (vlist);
free (streams);
}
这段代码可以顺利编译,但不能正常工作...例如,如果我编写以下完整代码:
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
void vffprintf (int z, ...)
{va_list vlist, auxvlist, auxauxvlist;
FILE **streams = malloc (z * sizeof(FILE *));
va_start (vlist, z);
for (int i = 0; i < z; ++i)
{streams[i] = va_arg (vlist, FILE *);
}
char const *format = va_arg (vlist, char const *);
va_copy (auxvlist, va_arg (vlist, va_list));
for (int i = 0; i < z; ++i)
{va_copy (auxauxvlist, auxvlist);
vfprintf (streams[i], format, auxauxvlist);
va_end (auxauxvlist);
}
va_end (auxvlist);
va_end (vlist);
free (streams);
}
void printf_variant (char const *format, ...)
{va_list vlist;
va_start (vlist, format);
vffprintf (1, stdout, format, vlist);
va_end (vlist);
}
int main (void)
{printf_variant ("Ramanujan's number is %d.\n", 1729);
return 0;
}
我得到一个段错误!...为什么?!
P.-S.:很抱歉这个很长的问题;但我希望它非常清楚,因为它相当技术性......
P.-S.2:对于这个问题,我特意使用了标签“va-list”和“variableargumentlists”,因为我感兴趣的是va_list,被视为一种类型,在里面一个(其他)变量参数列表,视为一个列表...所以这里实际上是两个不同的概念。
【问题讨论】:
-
那么问题是为什么最后一个例子失败了?
-
是的,就是这样:-)
-
如果忽略内存泄漏,看起来还不错。
-
FILE **streams = malloc (z * sizeof(FILE *)); -
糟糕!我什至不知道我怎么会犯这样的错误...... :-S 更正它。
标签: c variadic-functions