Search code examples
clinux64-bitvariadic-functions

va_arg 64bit issue


I've such C code. On 64-bit linux system the result is: 4294967264 instead of -32. Both clang and gcc produce binary with same incorrect results. The problem in the line:

*v = va_arg(args, long);
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

void setter(long *v, ...)
{
        va_list args;
        va_start(args, v);
        *v = va_arg(args, long);
        va_end(args);
}

int main()
{
        long v = 0;
        setter((long *) &v, -32);
        printf("%ld\n", v);
        return 0;
}

Solution

  • You actually need to pass a long to your function. You're passing an int.

    setter(&v, -32L);