Solved

variable-argument lists

Posted on 2000-03-18
9
286 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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2638855
Try use char * as printf
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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