Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are variable length argument lists implemented?

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?

like image 632
Paul Manta Avatar asked Dec 26 '11 20:12

Paul Manta


People also ask

How do you implement variable length arguments in Python?

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.

What is variable length argument list?

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.

How do we implement variable sized argument in generic programming?

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[].


1 Answers

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.

like image 83
Oliver Charlesworth Avatar answered Oct 13 '22 00:10

Oliver Charlesworth