A bit different handling of variable number of arguments.

Exclusive to C

Dealing with a variable number of arguments is pretty easy in C++.

Alas in real life, you will need to dive into C, from time to time.

And likely meet C/C++ macros with a variable number of arguments, or even worse sounding “Variadic Macro’s“. But why would anybody need any of this?

Like C++ is not “hard enough” already, one might exclaim? Well, standard C++ is not that hard.  But to learn anything properly, good examples are priceless. Here is perhaps one good example aka “use case” where one can deploy variadic macros.

(In case you are in a hurry, Godbolt is here)


Sooner or later you realize your C code will be full of malloc() calls. It is extremely important to get into the habit of free() ing, anything that should be freed. Just free-free-free, as much as you can. Being involved with standard C too, I  have developed my little function to “free in bulk”.   Next is the “why” part, and the test code.

First here is some heap-allocated data, that requires a lot of free() calls afterwards.

In C there are no destructors, so you better be sure you have cleaned that up before you have left. A good example is code like the above, with a mandatory, long, and tedious sequence of calls to the standard function free() that has to come after it :

This is not very nice. And more importantly, that is a bit tiresome, so it is human error-prone. So, I have developed a variadic function with a signature like this:

Please do not miss that note above. And, I have developed a variadic macro with a normal name to use this useful function:

That syntax is simple. As is the usage standard inbuilt predefined __VA_ARGS__ that will become the arguments list expanded. Now I can ease (a bit), the burden of repeated free calls:

But. Dear CLANG (aka LVVM) in its standard C reincarnation screams at me now:

Hmm, casting each argument will be as tedious as typing separate free calls, if not even  more tedious:

Not acceptable.  So, I have tweaked my variadic macro:

First argument? This macro now expands into:

Just the first argument is cast as requested.

And that first (void *) cast pacifies the compiler since the remaining args types are not checked before they are used inside the variadic function.

I bet that comes as a surprise.

And (lo and behold) there is no warning and all is dandy (in the land of macro candy).

But what about this annoying little comment we keep on dragging?

/* do not ever forget NULL as the last argument! */

Standard C, variadic functions implementation is based on what is available in <stdarg.h >. There is no mechanism in there to tell us which is the last argument in standard C variadic functions.  In this case, we need to use the concept of the “last argument sentinel”. The last argument is therefore a special value (sentinel) that signals the list of arguments is finished. And since the type of every argument is void * , NULL is the only logical choice.

Fine. I then do the last and final version of my FREE variadic macro, by simply adding the NULL argument in the call:

A function free_free_set_them_free is implemented to stop if the argument is NULL. This is one very good example of why sometimes we need variadic macros and how to use them. The call to MULTIFREE looks now pretty normal, and a good replacement for repeated calls to free().

No casting and no last argument sentinel necessary.

I am sure you can extrapolate this to your various use cases, with various variadic functions in standard C.

But the better code is here. It does not exactly match this post. It is better.

Warning. That godbolt example contains good C. Make sure to visit, ask questions and use for your use cases.

Wait, there is more!

Let us start with the C function signature, which has an array as an argument.

This might be tedious to call. Try.

Should we do it just with C va_arg and friends?  If you ask any C expert, <stdarg.h> is actually to be avoided. The key problem is that the stdarg.h APIs are imposing (on the user) the requirement to “tell” explicitly the length of the arguments list, use a sentinel for the last argument or have special logic like printf(const char *, ...) ;

printf is “simply” peeking into the first argument to decide on the number of arguments required.

However, using variadic macros in C, you do not need to use stdarg.h (always).  How? Again: Combine standard variadic macro with a function call

It is a macro that expands its arguments to a function call. Calling a function with a special function signature. Ok, how do we apply that here? fiddling with “va macros” from the <stdarg.h>. In the native array, all elements are of the same type.)

And through the magic of variadic macro, we make the whole lot, kind of comfortable. Here is the one I prepared earlier.

VARICALL is deliberately not short and a bit annoying name. So you remember it is a macro. If that is all confusing here is my explanation. This is standard C.

C++ can not do compound literals.

Standard C compound literal is used to declare arrays inline. Thus doing this (only inside some function please) :

Will expand into:

Where that funcint is declared as above:

Yes, that is legal standard C, array argument declaration, requiring a native array of at least size_ elements. And that is the usage with the help of a macro:

That is all you need. #include <stdarg.h> not required. For a reasonably large number of use cases, I think that is a very useful little idiom.

And of course, do not forget that Godbolt for something completely different.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.