10

I want to make varargs function for freeing multiple pointers at once, mostly to clean up the code. So I have:

void free_all( ... ) {
    va_list arguments;
    /* Initializing arguments to store all values after last arg */
    // but there are no args!
    va_start ( arguments, ????? );
    /* we expect the caller to send last argument as NULL **/
    void* pointer = va_arg ( arguments, void* );
    while( (pointer = va_arg ( arguments, void* ))!=NULL ) {
        free(pointer);
    }
    va_end ( arguments );                  // Cleans up the list
}

So what to put in va_start ( arguments, ????? )?

1 Answer 1

8

It's simply not possible. You MUST have a non vararg argument, always. In your case

void free_all(void *first, ...);

could work.

Sign up to request clarification or add additional context in comments.

7 Comments

Is that a technical limitation, or simply something not implemented? (Here I'm simply curious how it works)
@TomášZato, it's an oversight by the standard IMO. That extra argument is a hook into the stack frame. Theoretically, it could be accomplished without it.
@StoryTeller I was going to say it couldn't, but then I thought exactly of that. There must be a historical reason though.
@iharob, they probably reasoned that the feature can't be useful without sending some information about the extra parameters. And that translated to requiring at least one fixed parameter.
@TomášZato: It's simply because the C standard says so. You could make your own extension of C that supports this somehow, but it wouldn't be standard C.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.