What happens internally when a functions that uses varargs is called? Are the arguments themselves stored on the heap or on the stack like any other arguments. If on the stack, how does that work?
To provide variable-length parameters, we need to use an asterisk before the parameter name in the given method. The type of parameters supplied is a tuple, and within the method, these passed arguments form a tuple with the same name as the parameter, excluding the asterisk.
Variable length argument is a feature that allows a function to receive any number of arguments. There are situations where we want a function to handle variable number of arguments according to requirement.
Syntax of VarargsA variable-length argument is specified by three periods or dots(…). This syntax tells the compiler that fun( ) can be called with zero or more arguments. As a result, here, a is implicitly declared as an array of type int[].
It's implementation-dependent. But most probably, the args are placed on the stack, one after the other (after default argument promotions have been performed).
va_start
, va_arg
etc. work by simply walking a pointer through the stack, and reinterpreting the bits as whatever type you ask for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With