具有可变参数的函数是否需要调用 va_start?

Are functions with variadic arguments required to call va_start?

在开始使用 va_list 之前尽早退出具有可变参数的函数是否安全?

#include <cstdarg>

int func(const char * format, ...){
    if(format == NULL)
        return 0; // <-- exits before acknowledging variadic parameters; is this okay?
    va_list params;
    va_start(params, format);

    // func body

    va_end(params);
    return stuff;
}

,这是合法的。 ,函数不需要调用va_start。来自 C99 标准:

If access to the varying arguments is desired, the called function shall declare an object ... having type va_list.

这里注意两件事:

  1. va_listva_start 调用的先决条件。
  2. A va_list 只需要 如果 需要访问不同的参数。

因此,仅需要 va_start 调用 如果 需要访问不同的参数。