Question

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?

Was it helpful?

Solution

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, ...)

OTHER TIPS

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.

You used a varargs macro in a function with a fixed number of arguments; MSDN link to the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top