Variadic function: Difference between revisions

Content deleted Content added
m fix links
Line 9:
Variadic functions can expose type-safety problems in some languages. For instance, C's <TT>printf</TT>, if used uncautiously, can give rise to a class of security holes known as [[format string attack]]s. The attack is possible because the language support for variadic functions is not [[type safety|type-safe]]; it permits the function to attempt to pop more arguments off the stack than were placed there -- corrupting the stack and leading to unexpected behavior.
 
== Variadic functions in [[C (programming language)|C]] and [[C++]] ==
 
To portably implement variadic functions in the C programming language, the standard [http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html <tt><stdarg.h></tt>] header file should be used. The older [http://www.opengroup.org/onlinepubs/007908799/xsh/varargs.h.html <tt><varargs.h></tt>] header has been deprecated in favor of [http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html <tt><stdarg.h></tt>].
Line 39:
In ''PHP'', [http://php.net/functions_arguments variable-length argument lists] are natively supported (without security risk) since version 4; dedicated functions (<TT>func_num_args</TT>, <TT>func_get_arg</TT>, <TT>func_get_args</TT>) allow the programmer to determine the number and values of unspecified arguments.
 
== Variadic functions in [[C Sharp|C#]] and [[Java (programming language)|Java]] ==
 
Other languages, such as ''C#'' and ''Java'' use a different approach - they just allow that a variable number of arguments of the same (super)type may be passed to a variadic function. Inside the method they are simply collected in an [[array]]. Using [[autoboxing]], this can also be used to pass [[Primitive type|primitive]] arguments to for example a print function, as can be seen in the Java example below: