Variadic function: Difference between revisions

Content deleted Content added
Chip Zero (talk | contribs)
section titles, java example with autoboxing, removed stub
Chip Zero (talk | contribs)
move php to new section, C together with C++ in one section
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]] 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>].
 
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>].
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++]] ==
 
The variadic function feature is going to be present in the upcoming ''C++'' language standard, [[C++0x]]; this feature is called "Variadic templates". This allows to create templates with variable number of template parameters, which can be also used to create a function with variable number of arguments:
Line 29 ⟶ 27:
print( args... );
}
 
== Variadic functions in [[PHP]] ==
 
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]] ==