?
Solved

variable-argument lists

Posted on 2000-03-18
9
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 1

Accepted Solution

by:
ramshank earned 150 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

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