Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of compilation error : error C2704: __va_start intrinsic only allowed in varargs?

I'm using VS2010 Pro compiler, when I build on x64 I get below compilation error. Compiles perfectly on x86. error C2704: '' : __va_start intrinsic only allowed in varargs

Declaration of method:

int foo(char* buf, int maxChar, const char*& fmt);

definition:

 int foo(char* buf, int maxChar, const char*& fmt)
{
    int numChar = 0;
    if (fmt)
    {
        va_list plist;
        va_start(plist, fmt);
        numChar = _vsnprintf(buf, maxChar, fmt, plist);
        va_end(plist);
    }
    return numChar;
}

What is the meaning of the error? How to fix this?

like image 261
Coder777 Avatar asked Dec 19 '22 23:12

Coder777


2 Answers

I think it means pretty much what it says. The compiler won't allow you to use va_start, va_arg, etc, except in a variable argument function. Using va_start outside of a vararg function makes no sense.

This doesn't define a variable argument function:

int foo(char* buf, int maxChar, const char*& fmt)

This does:

int foo(char* buf, int maxChar, const char*& fmt, ...)
like image 118
Jonathan Potter Avatar answered Mar 23 '23 01:03

Jonathan Potter


On x86, all arguments are passed on the stack, and it's semantically safe (albeit incorrect) to use va_start and friends to get "arguments".

However, on amd64 (and most likely on ARM), some arguments are passed via registers. In this case, using va_start in a function that isn't declared to take variable arguments is semantically unsafe - va_start would index into invalid memory.

like image 39
Eric Brown Avatar answered Mar 22 '23 23:03

Eric Brown