如何正确va_end?

How to properly va_end?

#include <cstdarg>
using namespace std;

void do_something( va_list numbers, int count ) {
//                         ^
// Should I call this by reference here? I mean, va_list & numbers?
    //... stuff
    va_end( numbers );
}

void setList( int count, ... ) {
    va_list numbers;
    va_start( numbers, count );
    do_something( numbers, count );
}

int main() {
    setList( 2, 0, 1 );
    return 0;
}

当把va_list传给另一个函数时,我应该如何将它传递给那个函数?我知道当使用 va_list 完成事情时必须调用 va_end 我很困惑是否应该通过引用调用它。 va_list 即使没有通过引用调用也会正确结束吗?

我更喜欢在调用 va_start 或 va_copy 的同一函数中调用 va_end,就像任何其他资源 allocation/deallocation 对一样。这种风格是标准所要求的,尽管有些实现更宽容。

通常,va_lists 是按值传递的。它们是小物体。

可变参数列表:

永远不要在以 va_list 作为参数的函数中使用 va_end

来自 va_arg 的 (Linux) 手册页:

Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function.

(强调我的)

在具有 va_list 参数的函数中,按值获取 va_list(作为 C 库函数,如 vprintf,...也是如此)。

示例:

#include <cstdarg>
#include <iostream>

void v_display_integers(int count, va_list ap) {
    while(0 < count--) {
        int i = va_arg(ap, int);
        std::cout << i << '\n';
    }
}

void display_integers(int count, ...) {
    va_list ap;
    va_start(ap, count);
    v_display_integers(count, ap);
    va_end(ap);

    // You might use 'va_start' and 'va_end' a second time, here.
    // See also 'va_copy'.
}

int main() {
    display_integers(3, 0, 1, 2);
}

注:

在 C++ 中,您应该避免使用可变参数列表。备选方案是 std::arraystd::vectorstd::initializer_list 和可变参数模板。

宏函数 va_end 实现为 void 语句或重置 va_list 变量。

http://research.microsoft.com/en-us/um/redmond/projects/invisible/include/stdarg.h.htm

理论上,你可以在任何地方调用它,甚至可以跳过它。但是,要成为一名优秀的程序员,我们需要将它放在 va_ 函数之后