我们可以调用 va_start() 两次而不调用 va_end() 吗?
Can we call va_start() twice without calling va_end() in between?
这是我的最小示例:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
void print_strings_and_lengths(int count, ...)
{
va_list ap;
/* Print strings */
va_start(ap, count);
for (int i = 0; i < count; i++) {
char *s = va_arg(ap, char *);
printf("%d - %s\n", i, s);
}
/* Print string lengths */
va_start(ap, count); /* Is it okay to call va_start() again without calling va_end()? */
for (int i = 0; i < count; i++) {
char *s = va_arg(ap, char *);
printf("%d - %zu\n", i, strlen(s));
}
va_end(ap);
}
int main()
{
print_strings_and_lengths(3, "apple", "ball", "cat");
return 0;
}
此代码在同一个变量参数列表上调用 va_start()
两次。两次调用之间不调用 va_end()
函数。此代码是否定义明确或是否调用了未定义的行为?
C11 7.16.1/1:
[...] Each invocation of the va_start
and va_copy
macros shall be matched by a corresponding invocation of the va_end
macro in the same function.
两个 va_start
调用都没有相应的 va_end
,因此代码会导致未定义的行为,不需要诊断,因为上述引用不是约束的一部分。
这是我的最小示例:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
void print_strings_and_lengths(int count, ...)
{
va_list ap;
/* Print strings */
va_start(ap, count);
for (int i = 0; i < count; i++) {
char *s = va_arg(ap, char *);
printf("%d - %s\n", i, s);
}
/* Print string lengths */
va_start(ap, count); /* Is it okay to call va_start() again without calling va_end()? */
for (int i = 0; i < count; i++) {
char *s = va_arg(ap, char *);
printf("%d - %zu\n", i, strlen(s));
}
va_end(ap);
}
int main()
{
print_strings_and_lengths(3, "apple", "ball", "cat");
return 0;
}
此代码在同一个变量参数列表上调用 va_start()
两次。两次调用之间不调用 va_end()
函数。此代码是否定义明确或是否调用了未定义的行为?
C11 7.16.1/1:
[...] Each invocation of the
va_start
andva_copy
macros shall be matched by a corresponding invocation of theva_end
macro in the same function.
两个 va_start
调用都没有相应的 va_end
,因此代码会导致未定义的行为,不需要诊断,因为上述引用不是约束的一部分。