1

What are the benefits of using variadic functions

void fun(int i, ...);

instead of passing a pointer to an array?

void fun(int i*);

When are variadic functions preferred?

Man of One Way
  • 3,904
  • 1
  • 26
  • 41
  • possible duplicate of [What are variadic functions in accordance with C and C++?](http://stackoverflow.com/questions/3870128/what-are-variadic-functions-in-accordance-with-c-and-c) – iammilind Sep 15 '11 at 08:35

4 Answers4

6

You have to explicitly create the array first. In addition, it would be problematic if you wanted to specify parameters of different types.

Variadic functions do not require creating any array and they can deal with different types.

As an example, I could not bare to use the printf-functions if I always had to create some array first.

On the other hand, in most cases it's just a form of syntactic sugar, I suppose.

robert
  • 3,484
  • 3
  • 29
  • 38
  • 1
    It's especially sugary for the caller. The callee has to use the varargs macros anyway, and implementing your own version of `va_arg` for some scheme of your invention that packs a mixed-type bunch of values into a char buffer isn't too hard. The fiddly bit is getting the alignment right. So the callee would look fairly similar if you had to do it yourself, just take a char* param instead of calling `va_start`. But then the caller would have to pack the values into the buffer one by one, and as you say that would make calls to `printf` unbearably verbose. – Steve Jessop Sep 15 '11 at 08:50
2

Pointer to array assumes predefined type of the parameter (or struct, if its several different types).

Variadic functions are used when you don't know ahead of time what would the type of the parameter be, and you use a hint of the predefined parameters to get that knowledge (like the format string for printf).

littleadv
  • 20,100
  • 2
  • 36
  • 50
2

Also, you don't want to pass an array in a variadic function, as you would also want to pass in its size. e.g:

myfunction(int *pArray, int NumElements)

The main point, though, is that variadic functions allow many different types to be passed.

noelicus
  • 14,468
  • 3
  • 92
  • 111
2

I would suggest you to simply not use variadic functions. However they can be useful. For example in template metaprogramming techniques to implement compile-time querys.

  1. As the compiler can't verify that a given call to a variadic function passes an appropriate number of arguments or that those arguments have appropriate types. Consequently, a runtime call to a variadic function that passes inappropriate arguments yields undefined behavior.
  2. In pure C environments variadic functions have no alternative but in c++ you are able to find object oriented alternatives (also one issue).
  3. Variadic functions are not trivial to implement. Failing to initialize ap, calling va_arg() one time to many or omitting the va_end(ap) call can crash your program.

Example of being close to undefined behavior:

It was my job to crate a object-oriented wrapper around the sqlite C api. Here I am: I created a fancy interface for executing sqlite querys it was something like that:

void Query(const std::string& Query, void* Arguments, ...);

This is on one side awesome and fancy but: You immediately encounter undefined behavior if your parameter types are wrong. For example:

db->Query("select * from northwind where id = ?", "Peter"); // espects int as type

Will result in undefined behavior.

Mythli
  • 5,995
  • 2
  • 24
  • 31