va_list dynamically 
Author Message
 va_list dynamically

Hello!
Everybody knows the (...)-parameter-list, but does anybody know, how to
build up such an list dynamically.
Why do I ask this? I do not know the format-string for sprintf at
compile-time, neither the list of arguments, so i have to build up a
va_list from a list of parameters at runtime (for example a list of
VARIANTs) and pass it to vsprintf.

Thanks,
    Oliver Hoehle
--



Sat, 18 May 2002 03:00:00 GMT  
 va_list dynamically

Quote:

> Everybody knows the (...)-parameter-list, but does anybody know, how to
> build up such an list dynamically.

Can't be done portably.

Quote:
> Why do I ask this? I do not know the format-string for sprintf at
> compile-time, neither the list of arguments, so i have to build up a
> va_list from a list of parameters at runtime (for example a list of
> VARIANTs) and pass it to vsprintf.

Why can't you just build up a string piece by piece?  This would
be more portable and probably clearer as well.

If you want more detailed advice you'll have to post a more
detailed question.

You post here, you read here.
--
Peter Seebach on managing engineers:
"It's like herding cats, only most of the engineers are already
 sick of laser pointers."
--



Sun, 19 May 2002 03:00:00 GMT  
 va_list dynamically

Quote:

> Hello!
> Everybody knows the (...)-parameter-list, but does anybody know, how to
> build up such an list dynamically.
> Why do I ask this? I do not know the format-string for sprintf at
> compile-time, neither the list of arguments, so i have to build up a
> va_list from a list of parameters at runtime (for example a list of
> VARIANTs) and pass it to vsprintf.

There's no way to do what you want portably.
However there's no reason that you can't build up the string one
piece at a time.  Conveniently, sprintf returns the number of
chars it printed, simplifying this task.

e.g.

char buf[2048];
char *cur = buf;
char *end = buf + sizeof(buf) - 1;

cur += sprintf (cur, "%d", number); /* check to prevent overflow missing */
cur += sprintf (cur, "%.*s", end - cur, string); /* to prevent buffer overflow */
cur += sprintf (cur, "%x", hex); /* check to prevent overflow missing */
.. . .

Sorry -- if I respond to a post I'm going to share that info with
everyone who reads this group.

/peter
--



Sun, 19 May 2002 03:00:00 GMT  
 va_list dynamically
There is no portable way to build a va_list
dynamically.  Here's a dirty hack that might
do the trick.  It uses a struct that serves
as a "fake stack" and asks the compiler
to pass it by value to printf().  That way,
you can build both the format and the
argument list dynamically.

#if defined(USE_PORTABLE_CODE) || \
    defined(I_LOVE_MY_JOB)
Too Bad
#endif

#include <stdio.h>

void main()
{
  struct { char buf[100]; } stack;
  int a = 6, b = 7, c = 2;
  double d = 42.0;
  char *ptr = stack.buf;
  char *format = "%d x %d = %.*f\n%s\n";
  char *text = "And thanks for all the fish!";
  memcpy( ptr, &a, sizeof(int) );
  ptr += sizeof(int);
  memcpy( ptr, &b, sizeof(int) );
  ptr += sizeof(int);
  memcpy( ptr, &c, sizeof(int) );
  ptr += sizeof(int);
  memcpy( ptr, &d, sizeof(double) );
  ptr += sizeof(double);
  memcpy( ptr, &text, sizeof(char*) );
  ptr += sizeof(char*);  
  printf( format, stack );

Quote:
}

--



Sun, 19 May 2002 03:00:00 GMT  
 va_list dynamically

Quote:

> Everybody knows the (...)-parameter-list, but does anybody know, how to
> build up such an list dynamically.

Your compiler writer usually does, but no-one else. Or, to put it
differently: you cannot do that in an anywhere near portable program.
It's 100% implementation dependent, and you had better avoid the need
for doint it, instead...

--

Even if all the snow were burnt, ashes would remain.
--



Sun, 19 May 2002 03:00:00 GMT  
 va_list dynamically
# There is no portable way to build a va_list
# dynamically.  Here's a dirty hack that might
# do the trick.  It uses a struct that serves
# as a "fake stack" and asks the compiler
# to pass it by value to printf().  That way,
# you can build both the format and the
# argument list dynamically.

# #include <stdio.h>

# void main()

I think we told you about 2^64 times that 'void main' is a no-no.
Why is it you keep posting erroneous code?

# {
#   struct { char buf[100]; } stack;
#   int a = 6, b = 7, c = 2;
#   double d = 42.0;
#   char *ptr = stack.buf;
#   char *format = "%d x %d = %.*f\n%s\n";
#   char *text = "And thanks for all the fish!";
#   memcpy( ptr, &a, sizeof(int) );
#   ptr += sizeof(int);
#   memcpy( ptr, &b, sizeof(int) );
#   ptr += sizeof(int);
#   memcpy( ptr, &c, sizeof(int) );
#   ptr += sizeof(int);
#   memcpy( ptr, &d, sizeof(double) );
#   ptr += sizeof(double);
#   memcpy( ptr, &text, sizeof(char*) );
#   ptr += sizeof(char*);  
#   printf( format, stack );
# }


tst.c:5: warning: return type of `main' is not `int'

zsh: segmentation fault (core dumped)  ./a.out

tst.c:5: warning: return type of `main' is not `int'

zsh: segmentation fault (core dumped)  ./a.out

tst.c:5: warning: return type of `main' is not `int'

zsh: segmentation fault (core dumped)  ./a.out

tst.c:5: warning: return type of `main' is not `int'

-4261424 x 0 = 0

I know you said your code is non-portable. On a sun4u sparc SUNW,Ultra-1
at least, gcc produces programs that don't work as intended without
optimization and dump core with any optimization-level...

Was it too difficult to tell us what architecture your code
was intended for?

Regards,

        Jens
--
Jens Schweikhardt  http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)
--



Mon, 20 May 2002 03:00:00 GMT  
 va_list dynamically

...

Quote:
>e.g.

>char buf[2048];
>char *cur = buf;
>char *end = buf + sizeof(buf) - 1;

>cur += sprintf (cur, "%d", number); /* check to prevent overflow missing */
>cur += sprintf (cur, "%.*s", end - cur, string); /* to prevent buffer overflow
> */

* modifiers require a corresponding int argument. end - cur has type
ptrdiff_t which is not necessarily int. You need to cast to make this
correct

 cur += sprintf (cur, "%.*s", (int)(end - cur), string);

--
-----------------------------------------


-----------------------------------------
--



Sat, 25 May 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. dynamically constructing a va_list

2. a va_list in a va_list

3. Problem with va_list in VC++ 6.0/2002/2003 Final Beta

4. va_list ??

5. va_list, structure parameters, casting

6. Dynamic building of va_list ?

7. va_list passed by value/reference

8. static va_list

9. warning message with va_list

10. Problem compiling with va_list

11. Array copy/va_list/pointer prob

12. Re-using a va_list

 

 
Powered by phpBB® Forum Software