Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Var Args are funky

Name: Anonymous 2007-05-21 0:19 ID:vdiiFf1F

When I did a return from va_arg(args, void *), it always seems to return null even though there are more arguments remaining. In this case, the arg list is filled with vars that need to be free'd prior to exiting the program. Anyone know why such an unsightly approach is needed?

int cp_error(const char *msg, int varc, ...) {
    int varg = 0;
    va_list args;

    if (varc > 0) {
        varg = 1;
        va_start(args, msg);
    }

    while (--varc >= 0) {
        va_arg(args, void *);
        free( (void *) *((char **) args) );
    }

    if (varg) {
        va_end(args);
    }

    fprintf(stdout, "%s\n", msg);

    return 0;
}

Name: Anonymous 2007-05-21 2:35 ID:KsDjH0VH

There's a bunch of AIDS in this code. First, you need to call va_start() and va_end() regardless of whether actual arguments are present or not. Second, va_arg() _returns_ a value and _alters_ the va_list parameter.

So what you want to do is remove varg, both of the if blocks (leave va_start() and va_end()) and instead call free(va_arg(args, void *)) in the loop.

Enough asswiping for you? RTFM the next time around, shithead.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List