Solved

variable-argument lists

Posted on 2000-03-18
9
295 Views
Last Modified: 2010-05-18
Here's my question:  I'm trying to create a custom printf-like function that processed a string as input that also contains format specifiers.  Obviously this function requires a variable-length argument list.

Everything works out fine, except for one thing:  When I run the program (the one below is a fragment of my actual program), the printf-like function processes and displays the FIRST argument correctly.  Second and subsequent arguments don't get displayed properly (ex. garbage on screen).  What to do??

////////////////////////////////////////
vector<string> g_vecStrStats; //global

template<class T>
void gAppendStats(string szStat, int iCount, T pvFirst, ...)
{
   string  szResult= szStat;
   TCHAR   sz[50];
   va_list ap;
   int     iCurrPos= 0;
   int     iCurrPos2;

   va_start(ap, iCount);
   T pv= va_arg(ap, T);

   //Skip all chars until a '%' is
   //found.
   while(szResult[iCurrPos] != '%')
      ++iCurrPos;

   //iCurrPos should now represent the
   //first '%' position.
   iCurrPos2= iCurrPos;
   while(iCurrPos2 != szResult.length())
   {
      if (szResult[iCurrPos2] == 'd')
      {
         ltoa(pv, sz, 10);
         szResult.replace(iCurrPos,
         iCurrPos2 - iCurrPos + 1, sz,
         0, strlen(sz));
         break;
      }

      else if (szResult[iCurrPos2] ==
               'f')
      {
         gcvt(pv, 5, sz);
         szResult.replace(iCurrPos,
         iCurrPos2 - iCurrPos + 1, sz,
         0, strlen(sz));
         break;
      }

      ++iCurrPos2;
   }
   iCurrPos= iCurrPos2;

   //Process second and subsequent
   //variable args.
   for (int i= 1; i < iCount; i++)
   {
      T pv= va_arg(ap, T);

      //Skip all chars until a '%' is
      //found.
      while(szResult[iCurrPos] != '%')
         ++iCurrPos;

      //iCurrPos should now represent
      //the next '%' position.
      iCurrPos2= iCurrPos;
      while(iCurrPos2 != szResult.length
      ())
      {
         if (szResult[iCurrPos2] == 'd')
         {
            ltoa(pv, sz, 10);
            szResult.replace(iCurrPos,
            iCurrPos2 - iCurrPos + 1,
            sz, 0, strlen(sz));
            break;
         }

         else if (szResult[iCurrPos2]
                  == 'f')
         {
            gcvt(pv, 5, sz);
            szResult.replace(iCurrPos,
            iCurrPos2 - iCurrPos + 1,
            sz, 0, strlen(sz));
            break;
         }

         ++iCurrPos2;
      }
      iCurrPos= iCurrPos2;
   }

   //Finally, add this processed string
   //stat in the vector of strings and
   //perform cleanup.
   g_vecStrStats.push_back(szResult);
   va_end(ap);
}

void main()
{
   long  lNumBufs = 2;
   float fBitNum  = 16.578f;

   gAppendStats("Detected %d %f-bit Z
   buffers", 2, lNumBufs, fBitNum);

   //app cleanup
   g_vecStrStats.clear();
}
0
Comment
Question by:metal3dx
  • 4
  • 4
9 Comments
 
LVL 1

Accepted Solution

by:
ramshank earned 50 total points
ID: 2632646
try giving the last specifed arguement in the list pvFirst in va_start instead of iCount.

va_start(ap, iCount);
the follwing the definition of the macro

void va_start( va_list arg_ptr, prev_param );  

va_start sets arg_ptr to the first optional argument in the list of arguments passed to the function.  The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro’s behavior is undefined.

In the code that you had specified you pass iCount which is not the required parameter immediately preceeding the optional parameter list.  Try changing that to pvFirst
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2636444
Hi. I tested you example:
 You can't use in same loop long/float!
>>   T pv= va_arg(ap, T);
How translator knows, what is type of this ?
Of course, by you declaration : "T".(long in example)
>>   gAppendStats("buffers", 2, lNumBufs, fBitNum);
but in same example next variable has type float(fBitNum)
and this is the reason of error!
For fix it you must make ALL float/int/long/void/void *, ...
Alex
0
 

Author Comment

by:metal3dx
ID: 2637425
To ramshank:

I've tried your idea before and the program still gave me incorrect results.

To AlexVirochovsky:

I've also tried your idea also.  As a matter of fact, using void* will give me a compiler error (at function gcvt, I will need a typecast from 'void*' to 'float' which is not allowed.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2638855
Try use char * as printf
0
 

Author Comment

by:metal3dx
ID: 2640736
Maybe I should restate the problem:

How do I create a printf-like function?
Obviously, a printf-like function will
have a variable length argument list AND is probably a template function so that it can support multiple basic data types.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2644682
Printf don't use template and use char *
as type of all parameters.
After that Printf tests format parameter and by this info detects real type of other parameters and convert to
symbol.
0
 

Author Comment

by:metal3dx
ID: 2662647
Does anybody out there have any sample code that demonstrates functions using variable argument lists like printf?
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2662880
Next is an example from BC doc:
/* va_arg example */

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

/* calculate sum of a 0 terminated list */
void sum(char *msg, ...)
{
   int total = 0;
   va_list ap;
   int arg;
   va_start(ap, msg);
   while ((arg = va_arg(ap,int)) != 0) {
      total += arg;
   }
   printf(msg, total);
   va_end(ap);
}

int main(void) {
   sum("The total of 1+2+3+4 is %d\n", 1,2,3,4,0);
   return 0;
}
Borland C++ 5.0 Programmer's Guide
0
 

Author Comment

by:metal3dx
ID: 2665421
Not what I was looking for but you still answered the question.

Nice job!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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

10 Experts available now in Live!

Get 1:1 Help Now