C 一个取 va_list 的函数是否可以多次使用它?

C Is it possible for a function taking va_list to use it more than once?

在下面的程序中,可变参数函数 process_message 通过 va_list argp 参数将其可变参数传递给 print_messageargp 依次传递给 vsnprintf,它计算格式字符串的长度。

但是,如果 argp 被传递给从 print_message 中调用的另一个函数,例如 vprintf(fmt, argp),它会产生无意义的输出。接受 va_list 参数的函数是否可以多次使用它?

#include <stdio.h>
#include <stdarg.h>

void process_message(const char *fmt, ...);
void print_message(const char *fmt, va_list argp);

int main(void) {
  process_message("%s:%d\n", "test message", 1);
  return 0;
}

void process_message(const char *fmt, ...) {
  va_list argp;
  va_start(argp, fmt);
  print_message(fmt, argp);
  va_end(argp);
}

void print_message(const char *fmt, va_list argp) {
  /*Calculate and print the length of the format string*/
  int len = vsnprintf(NULL, 0, fmt, argp);
  printf("Length of format string = %d\n", len);
  /*Print the rendered format string - this produces a nonsense output
   *if argp was aleady passed to another function earlier */
  vprintf(fmt, argp);
}

您可以使用 va_copy:

The va_copy() macro copies the (previously initialized) variable argument list src to dest. The behavior is as if va_start() were applied to dest with the same last argument, followed by the same number of va_arg() invocations that was used to reach the current state of src.

print_message() 可能是:

void print_message(const char *fmt, va_list argp) {
  /*Calculate and print the length of the format string*/ 
  va_list argp_copy;
  va_copy(argp_copy, argp);
  int len = vsnprintf(NULL, 0, fmt, argp_copy);
  va_end(argp_copy);

  printf("Length of format string = %d\n", len);
  /*Print the rendered format string - this produces a nonsense output
   *if argp was aleady passed to another function earlier */
  vprintf(fmt, argp);
}

注意:

不要忘记在使用复制的 va_list 后调用 va_end

C11:

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.