Solved

redirect args

Posted on 2011-02-17
14
437 Views
Last Modified: 2012-05-11
How to redirect args to different function

The reason for doing this is because the implemention has now moved from vout to vout_2. So I just need to redirect it to vout_2 (which is an external define)

The definition of vout_2 is

vout_2 (int, ## __VA_ARGS__)
#include <stdio.h>                                                              
#include <stdarg.h>                                                             
                                                                                
void vout(int max, ...);                                                        
void vout_2(int max, ...);                                                        
                                                                                
int main(void)                                                                  
{                                                                               
   vout(3, "Sat", "Sun", "Mon");                                                                           
}                                                                               

void vout(int max, ...)                                                         
{                                                                               
   // I want to call vout_2 by redirecting the args. What should be the parameter lis
   vout_2(max, <Not sure what should be here> )
}
                                                                                
void vout_2(int max, ...)                                                         
{                                                                               
   va_list arg_ptr;                                                             
   int args = 0;                                                                
   char *days[7];                                                               
                                                                                
   va_start(arg_ptr, max);                                                      
   while(args < max)                                                            
   {                                                                            
      days[args] = va_arg(arg_ptr, char *);                                     
      printf("Day:  %s  \n", days[args++]);                                     
      }                                                                         
   va_end(arg_ptr);                                                             
}

Open in new window

0
Comment
Question by:learningunix
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Make that like
void vout(int max,...)                                                         
{                                                                               
   va_list args;
   va_start(args,max);
 
   vout_2(max, args);

   va_end(args);
}

Open in new window

0
 

Author Comment

by:learningunix
Comment Utility
Here's what I tried but the output is garbage


Insie void_2
Day:
Day:  ØZ
Day:  Mon

#include <stdio.h>
#include <stdarg.h>

void vout(int max, ...);
void vout_2(int max, ...);

int main(void)
{
   vout(3, "Sat", "Sun", "Mon");
   printf("\n");
}

void vout(int max, ...)
{
   va_list arg_ptr;
   va_start(arg_ptr, max);
   vout_2(max, arg_ptr);
   va_end(arg_ptr);
}


void vout_2(int max, ...)
{
   va_list arg_ptr;
   int args = 0;
   char *days[7];

   printf("Insie void_2 \n");

   va_start(arg_ptr, max);
   while(args < max)
   {
      days[args] = va_arg(arg_ptr, char *);
      printf("Day:  %s  \n", days[args++]);
      }
   va_end(arg_ptr);
}

Open in new window

0
 
LVL 30

Accepted Solution

by:
Zoppo earned 250 total points
Comment Utility
Hi learningunix,

IMO this is not possible - the only thing you can do is to retrieve the va_list in 'vout' and pass it to 'vout_2', somehow like this:


void vout_2( int max, va_list& args ); 

void vout(int max, ...)                                                         
{                                                                               
 va_list args;
 va_start(args, max);

 vout_2( args );

 va_end(args);
}

void vout_2( int max, va_list& args )
{
  int args = 0;                                                                
  char *days[7];                                                               
                                                                                
  while(args < max)                                                            
  {                                                                            
   days[args] = va_arg( args, char *);                                     
   printf("Day:  %s  \n", days[args++]);                                     
  }                                                                         
}

Open in new window



I'm not sure if that somehow helps you, but I don't see any other possibility ...

ZOPPO
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Sorry, I needed to long to write before posting ...
0
 

Author Comment

by:learningunix
Comment Utility
hmmm... that means in C++ one cannot pass the arg list to other function.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Sorry, Zoppo is right - the 'void_2()' would have to take a 'va_list' as an argument.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Below is a program with output:
Sat
Sun
Monday

At each break at the printf statement (around Line 23), I took a memory dump of days[ i ] (which, as you can see, are collocated).Memory Dump: days[0], days[1], days[2]
I used VS 2010 Express, which, for C++ is an excellent debugger. If you use another compiler, it actually helps to compile using two systems as the error message on one may be easier to follow than on another.

If you haven't already done so, you can download the free Visual Studio Expresss C++ 2010:
    http://www.microsoft.com/express/Downloads/

I like the VS C++ debugger so much that I wrote articles on it. To quickly get started (about 15 minutes learning curve), you can read:

   C/C++ Beginner's Debugging Guide

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++


#include <stdio.h>
#include <stdarg.h>
#include <string.h>

void vout_2( int max, va_list &args );

void vout(int max ...)
{
   va_list args;
   va_start(args, max);

   vout_2( max, args );

   va_end(args);
}  

void vout_2( int max, va_list& args2 )
{
   char *days[7];
   for(int i = 0; i< max; ++i)
   {
      days[i] = va_arg( args2, char *);
      printf("%s\n", days[i]);
   }
}

int main() {
   vout( 3, "Sat", "Sun", "Monday");
}

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Sorry, phoffric, but I don't see relevant differences between my posted code and yours ...
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Sorry, Zoppo, but when I saw the comment
    "hmmm... that means in C++ one cannot pass the arg list to other function."
and
   "IMO this is not possible"
I thought the conclusion was just that. So, I guess I really misinterpreted what was being said, because passing the arg list down to another function is certainly doable.

I didn't evaluate your code because of these comments thinking there was a consensus that it was not doable.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
No problem - it's a bit confuzing naming two different things ('...', 'va_list') with the same name 'arg list' ... just a communication issue :o)
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
I guess I skimmed it too fast, and just missed the important gist of what you were saying. I realize now that you were just giving a schematic rather than compilable source.

So, I just compared our two posts a little more, and I see that I call vout_2 with two parameters whereas you call it with one; and what had confused me in my initial quick perusal was that in vout_2, I saw that line int args = 0 and the expression va_arg( args, char *) and thought that va_args should not be taking an int. (I see now that this latter issue is just a typo.)

Anyway, the main confusion was that I thought the consensus was that it was not doable. That's why I wrote a program with output just to make sure that it was doable.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Ah - sorry. Yes, that's a mistake in my (not tested) code - thanks for pointing that out.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
@learningunix: Just a question: Are both functions written by yourself or is there any reason that you have to use variable argument list?

I ask because IMO using a variable argument list like shown here is absoluteley senseless - passing the data as an array of 'char*' in the shown sample would be more flexible (because the number of elements isn't hardcoded in a function call) and more performant since the VA-handling isn't needed.

IMO variable argument lists only make sense when the type of passed parameters isn't fixed, i.e. as with 'printf', there the parameters can have any type. In a case like yours it's sure the expected parameters have to be of type 'char*', the only variant info is the number of parameters.

So, you could write your 'v_out' in one of these two ways:

void v_out_a( int max, ... );
void v_out_b( int max, char* args[] );

IMO the only advantage of 'v_out_a' is you can directly call it as shown in your original post, i.e.:

v_out_a( 3, "Sat", "Sun", "Mon" );

while with the second form you have to call it somehow like this:

char* args[] = { "Sat", "Sun", "Mon" };
v_out_b( 3, args );

This even has an atdvantage, since you can pass the size of the passed array in a more dynamically way, i.e.

v_out_b( sizeof( args ) / sizeof( args[ 0 ] ), args );

Thus changing the code to i.e. pass a fourth day isn't that error prone as the first version since with the first version you have to change the call to

v_out_a( 4, "Sat", "Sun", "Mon", "Tue" ); // Two changes needed

and for the second call you just have to modify the array

char* args[] = {  "Sat", "Sun", "Mon", "Tue"  };


ZOPPO
0
 

Author Closing Comment

by:learningunix
Comment Utility
thx
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maze travler 6 47
Want to delete all my personal data 13 126
Which IDE to use to begin C++ training? 5 56
computer science syllabus 3 52
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now