gpt4 book ai didi

c - va_copy() 是否需要 va_end()?

转载 作者:太空狗 更新时间:2023-10-29 14:58:57 31 4
gpt4 key购买 nike

在处理可变参数时,是只是 va_start() 需要匹配的 va_end() 调用,还是 va_copy()也需要匹配的 va_end(),即

void foo(char *x, ...)
{
va_list l,c;

va_start(l,x);
va_copy(c,l);

---
va_end(c); //is this correct ?
va_end(l);
}

最佳答案

是的,每个副本和原件都需要调用 va_end()。您的示例是正确的,您的 va_end() 调用顺序可以颠倒。

来自 ISO/IEC 9899:201x:

7.16.1.3 The va_end macro

The va_end macro facilitates a normal return from the function whose variable argument list was referred to by the expansion of the va_start macro, or the function containing the expansion of the va_copy macro, that initialized the va_list ap. The va_end macro may modify ap so that it is no longer usable (without being reinitialized by the va_start or va_copy macro). If there is no corresponding invocation of the va_start or va_copy macro, or if the va_end macro is not invoked before the return, the behavior is undefined.

最后一句指出每个 va_startva_copy 在返回之前必须伴随一个 va_end

关于c - va_copy() 是否需要 va_end()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283346/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com