va_list help

J

John Guo

Hi all,

I am trying to nest two functions both with variable length arguments.
(The only reason I nested them is that I called the outer function in
too many places, I don't want to replace every calls with the new one).

void outerFunction(const int arg1, const int arg2, ...)
{
int arg3;
va_list ap;
va_start(ap, arg2);
innerFunction(arg3, ap);
va_end(ap);
}
void innerFunction(int arg, ...)
{
va_list ap;
va_start(ap, arg);
char a[255];
strcpy(a, va_arg(ap,char*));
int x = va_arg(ap,int);
cout<<a<<endl;
cout<<x<<endl;
va_end(ap);
}

Say if I call outerfunction with

outFunction(1,2, "ProcessName", 5)

As I debug this, it seems that ap before passed in is ok to read using
va_arg(), but in innerfunction, it doesn't work, read out some garbage
in a and x.

Could someone give some comments on this? Any help is greatly
appreciated.

John
 
J

John Guo

That's similar to the prototype of vsprintf (i.e., take arguent va_list
ap).

If I do that, can the call to innerfunction directly still work?
e.g. innerFunction(1, 'sldafkdlasf", 2)
 
V

Victor Bazarov

John said:
I am trying to nest two functions both with variable length arguments.

That's generally impossible.
(The only reason I nested them is that I called the outer function in
too many places, I don't want to replace every calls with the new
one).

void outerFunction(const int arg1, const int arg2, ...)
{
int arg3;
va_list ap;
va_start(ap, arg2);
innerFunction(arg3, ap);
va_end(ap);
}
void innerFunction(int arg, ...)
{
va_list ap;
va_start(ap, arg);
char a[255];
strcpy(a, va_arg(ap,char*));
int x = va_arg(ap,int);
cout<<a<<endl;
cout<<x<<endl;
va_end(ap);
}

Say if I call outerfunction with

outFunction(1,2, "ProcessName", 5)

As I debug this, it seems that ap before passed in is ok to read using
va_arg(), but in innerfunction, it doesn't work, read out some garbage
in a and x.

Could someone give some comments on this? Any help is greatly
appreciated.

If you need some common code called from both functions, you will need
to defined a function that takes va_args (take a look at vfprintf in
the source for your standard library, or on the web somewhere). Make
both your functions that have ellipsis call that function (like the
'fprintf' does with 'vfprintf')

Good luck!

V
 
P

Pete Becker

John said:
That's similar to the prototype of vsprintf (i.e., take arguent va_list
ap).

Yup. Same problem, same solution.
If I do that, can the call to innerfunction directly still work?
e.g. innerFunction(1, 'sldafkdlasf", 2)

No. innerFunction now takes two arguments, one of type int and one of
type va_list.
 
P

Pete Becker

Pete said:
No. innerFunction now takes two arguments, one of type int and one of
type va_list.

I should add: often the best solution to splitting a function is not to
make two functions, but three.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top