在vsnprintf_s调用之后是否需要va_end?

Mr.*_*C64 7 c c++ variadic-functions

MSDN显示以下示例代码段vsnprintf_s:

// crt_vsnprintf_s.cpp
#include <stdio.h>
#include <wtypes.h>

void FormatOutput(LPCSTR formatstring, ...) 
{
   int nSize = 0;
   char buff[10];
   memset(buff, 0, sizeof(buff));
   va_list args;
   va_start(args, formatstring);
   nSize = vsnprintf_s( buff, _countof(buff), _TRUNCATE, formatstring, args);
   printf("nSize: %d, buff: %s\n", nSize, buff);
}

int main() {
   FormatOutput("%s %s", "Hi", "there");
   FormatOutput("%s %s", "Hi", "there!");
   FormatOutput("%s %s", "Hi", "there!!");
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,va_start调用时没有匹配va_end.

这是MSDN中的doc bug,还是应该在调用va_start 之前调用vsnprintf_s然后让这个函数va_end为我们进行清理(即调用)?

BTW:我尝试了上面的代码,它与VS2015一起使用Update 3,但我不知道它是否只是未定义的行为......

R S*_*ahu 7

va_end每个人都需要被召唤va_start.来自http://en.cppreference.com/w/c/variadic/va_end:

如果没有相应的调用va_startor va_copy,或者如果va_end在调用va_startva_copy返回的函数之前没有调用,则行为未定义.

你不仅需要va_end,但你还需要确保你的函数之前没有返回va_end,如果执行va_startva_copy执行.

回答你的问题 - 是的,这是MSDN中的文档错误.