[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

variable-argument lists

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
metal3dx
Asked:
metal3dx
  • 4
  • 4
1 Solution
 
ramshankCommented:
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
 
AlexVirochovskyCommented:
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
 
metal3dxAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlexVirochovskyCommented:
Try use char * as printf
0
 
metal3dxAuthor Commented:
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
 
AlexVirochovskyCommented:
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
 
metal3dxAuthor Commented:
Does anybody out there have any sample code that demonstrates functions using variable argument lists like printf?
0
 
AlexVirochovskyCommented:
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
 
metal3dxAuthor Commented:
Not what I was looking for but you still answered the question.

Nice job!!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now