variable num args via 'va_arg'

S

Suzanne Vogel

'stdarg.h' defines the 'va_arg' type to use in passing variable numbers
of parameters to functions. The example of its use given in the
Dinkumware documentation *seems* to imply that the 'va_arg' list should
*automatically* be terminated with NULL past the user's last argument:
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdarg.html

However, I (for some reason) have to pass in the count of the number of
arguments, or manually pass in NULL as the last argument. Otherwise, the
loop that extracts arguments from the 'va_arg' list runs past the end.

*** Should I *have* to manually pass in NULL or the number of arguments
in order to pass in a variable number of parameters using 'va_arg'?

Below is my test example, with the function call in main():

/******************************************************************************
* Demonstrate the use of variable numbers of parameters in a function call.
******************************************************************************/
#include <stdarg.h>
#include <iostream>

/******************************************************************************
* Declare a struct for use in avg() function.
******************************************************************************/
struct Double {
double value;
Double(double v) {
value = v;
}
};

/******************************************************************************
* Return the average of all parameters following 'n'.
*
* double avg(Double* x, [Double* x1], [Double* x2], ...)
******************************************************************************/
double avg(Double* x, ...) {
// Pass parameters as Double* instead of double or Double& because the
// following types are disallowed:
// * any array type
// * any function type
// * type float (e.g., double)
// * any integer type that changes when promoted
// * a reference type [C++ only]

va_list ap; // declare the parameter list
va_start(ap, x); // start reading the parameter list

double sum = x->value; // initialize sum and count
int count = 1;
while (Double* d = va_arg(ap, Double*)) {
sum += d->value;
count++;
}
va_end(ap); // stop reading the parameter list

return sum/(double)count; // return the average of all the values
}

/******************************************************************************
* Main function.
******************************************************************************/
int main(int argc, char** argv) {
// NOTE: I should *not* have to pass in NULL at the end, but otherwise
// the loop inside avg() continues 3 places past the end until it
finally
// reads in a NULL.
std::cout << avg(&Double(1.0), &Double(2.0), &Double(3.0),
&Double(4.0), &Double(5.0), &Double(6.0), NULL) <<
"\n";
}

Thanks,
Suzanne
 
J

John Harrison

Suzanne Vogel said:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers
of parameters to functions. The example of its use given in the
Dinkumware documentation *seems* to imply that the 'va_arg' list should
*automatically* be terminated with NULL past the user's last argument:
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdarg.html

I guess you could read it like that, but C++ makes no such guarntee.
However, I (for some reason) have to pass in the count of the number of
arguments, or manually pass in NULL as the last argument. Otherwise, the
loop that extracts arguments from the 'va_arg' list runs past the end.

*** Should I *have* to manually pass in NULL or the number of arguments
in order to pass in a variable number of parameters using 'va_arg'?

You have to do something to indicate the number of arguments. Passing in
NULL or the number of arguments are two obvious ways.

john
 
F

flekso

this is what <stdarg.h> has to say:

typedef char * va_list;

#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) -
1) )

#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
#define va_end(ap) ( ap = (va_list)0 )

so, i believe you should pass some kind of a marker or number of arguments
(in your case a 0 as the last argument)


Suzanne Vogel said:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers
of parameters to functions. The example of its use given in the
Dinkumware documentation *seems* to imply that the 'va_arg' list should
*automatically* be terminated with NULL past the user's last argument:
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdarg.html

However, I (for some reason) have to pass in the count of the number of
arguments, or manually pass in NULL as the last argument. Otherwise, the
loop that extracts arguments from the 'va_arg' list runs past the end.

*** Should I *have* to manually pass in NULL or the number of arguments
in order to pass in a variable number of parameters using 'va_arg'?

Below is my test example, with the function call in main():

/***************************************************************************
***
* Demonstrate the use of variable numbers of parameters in a function call.****************************************************************************
**/
#include <stdarg.h>
#include <iostream>

/***************************************************************************
***
* Declare a struct for use in avg() function.
****************************************************************************
**/
struct Double {
double value;
Double(double v) {
value = v;
}
};

/***************************************************************************
***
* Return the average of all parameters following 'n'.
*
* double avg(Double* x, [Double* x1], [Double* x2], ...)
****************************************************************************
**/
double avg(Double* x, ...) {
// Pass parameters as Double* instead of double or Double& because the
// following types are disallowed:
// * any array type
// * any function type
// * type float (e.g., double)
// * any integer type that changes when promoted
// * a reference type [C++ only]

va_list ap; // declare the parameter list
va_start(ap, x); // start reading the parameter list

double sum = x->value; // initialize sum and count
int count = 1;
while (Double* d = va_arg(ap, Double*)) {
sum += d->value;
count++;
}
va_end(ap); // stop reading the parameter list

return sum/(double)count; // return the average of all the values
}

/***************************************************************************
***
* Main function.
****************************************************************************
**/
int main(int argc, char** argv) {
// NOTE: I should *not* have to pass in NULL at the end, but otherwise
// the loop inside avg() continues 3 places past the end until it
finally
// reads in a NULL.
std::cout << avg(&Double(1.0), &Double(2.0), &Double(3.0),
&Double(4.0), &Double(5.0), &Double(6.0), NULL) <<
"\n";
}

Thanks,
Suzanne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top