Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to use varargs in a C API to set key value pairs?

Tags:

c

api

I am writing an API that updates a LOT of different fields in a structure.

I could help the addition of future fields by making the update function variadic:

update(FIELD_NAME1, 10, FIELD_NAME2, 20);

then later add FIELD_NAME3 with out changing any existing calls:

update(FIELD_NAME1, 10, FIELD_NAME2, 20, FIELD_NAME3, 30);

words of wisdom please?

like image 757
Arthur Ulfeldt Avatar asked May 14 '09 22:05

Arthur Ulfeldt


People also ask

Is Varargs good practice?

Varargs are useful for any method that needs to deal with an indeterminate number of objects. One good example is String. format . The format string can accept any number of parameters, so you need a mechanism to pass in any number of objects.

Can Varargs take an array?

Every time we use varargs, the Java compiler creates an array to hold the given parameters. In this case, the compiler creates an array with generic type components to hold the arguments. The varargs usage is safe if and only if: We don't store anything in the implicitly created array.

Is Varargs good in Java?

In summary, varargs parameters are a useful feature as part of the Java language. As with all features there are some rough edges to this feature that we should be aware of. However, as we keep these things in mind we can make some great improvements to our code.

What is Varargs?

Varargs is a short name for variable arguments. In Java, an argument of a method can accept arbitrary number of values. This argument that can accept variable number of values is called varargs.


1 Answers

Generally, no.

Varargs throws out a lot of type-safety - you could pass pointers, floats, etc., instead of ints and it will compile without issue. Misuse of varargs, such as omitting arguments, can introduce odd crashes due to stack corruption, or reading invalid pointers.

For instance, the following call will compile and result in crashes or other odd behavior:

UpdateField(6, "Field1", 7, "Field2", "Foo");

The initial 6 is how many parameters to expect. It will convert the string pointer "Foo" to an int to put in Field2, and it will try to read and interpret two other parameters that aren't present, which will probably cause a crash here from dereferencing stack noise.

I believe the implementation of varargs in C is a mistake (given today's environment - it probably made perfect sense in 1972.) The implementation is you pass a bunch of values on the stack and then the callee will walk the stack picking up parameters, based on its interpretation of some initial control parameter. This type of implementation basically screams for you to make a mistake in what might be a very difficult to diagnose way. C#'s implementation of this, passing an array of objects with an attribute on the method, is just must saner, albeit not directly mappable into the C language.

like image 62
Michael Avatar answered Sep 27 '22 15:09

Michael