Hi,
I have written a very simple program using variable arguments calls and
I
get strange things that I cannot explain.
I have one function (add) that displays two parameters. It works well
when I call it directly.
Now I have a second function (set) that also calls add. But this
doesn't
work.
Here is the output generated by my program. I don't understand why I
get different result (I tried on both Borland C++ Builder and in Visual
C++, and
I got similar results).
Any help would be greatly appreciated.
Thanks
Best Regards
Thierry ADD
format = H%dW%d
arg[] = 1234
arg[] = 5678
SET
format = H%dW%d
arg[] = 1245060
arg[] = 1245060
#include <stdio.h>
#include <stdarg.h>
//---------------------------------------------------------------------------
void add (char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
printf("format = %s\n",format);
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
}
//---------------------------------------------------------------------------
void set (char *format, ...)
{
va_list arg_ptr;
va_start (arg_ptr, format);
add(format, arg_ptr);
}
//---------------------------------------------------------------------------
int main()
{
printf("\n>>>ADD \n");
add("H%dW%d",1234,5678);
printf("\n>>>SET \n");
set("H%dW%d",1234,5678);
return 0;
} 11 2933
"thierrydollar" <th*****@tetraedre.com> writes: I have written a very simple program using variable arguments calls and I get strange things that I cannot explain.
I have one function (add) that displays two parameters. It works well when I call it directly. Now I have a second function (set) that also calls add. But this doesn't work.
Here is the output generated by my program. I don't understand why I get different result (I tried on both Borland C++ Builder and in Visual C++, and I got similar results).
I assume you were using both as C compilers; if you're using them as
C++ compilers, you're in the wrong newsgroup. Your program appears to
be within the common subset of C and C++, but there are subtle
differences.
[snip] ADD format = H%dW%d arg[] = 1234 arg[] = 5678 SET
format = H%dW%d arg[] = 1245060 arg[] = 1245060
#include <stdio.h> #include <stdarg.h>
//--------------------------------------------------------------------------- void add (char *format, ...) { va_list arg_ptr; va_start (arg_ptr, format);
printf("format = %s\n",format); printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long)); printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long)); } //--------------------------------------------------------------------------- void set (char *format, ...) { va_list arg_ptr; va_start (arg_ptr, format); add(format, arg_ptr); } //--------------------------------------------------------------------------- int main() { printf("\n>>>ADD \n"); add("H%dW%d",1234,5678);
printf("\n>>>SET \n"); set("H%dW%d",1234,5678);
return 0; }
Your call to add() is incorrect, but is likely to work anyway.
When it processes the call
add("H%dW%d",1234,5678);
the compiler doesn't know that add() is expecting unsigned long
arguments. The constants 1234 and 5678 are of type int. Try either
add("%dW%d", 1234UL, 5678UL);
or
add("%dW%d", (unsigned long1234, (unsigned long)5678);
or change the va_arg() invocations to use int.
This is likely to "work" if int and long are the same size, or if
they're passed with compatible calling conventions and the byte
ordering is just right.
(Also, since the standard *printf() functions use "%d" for type int,
it seems misleading for add() to use it for unsigned long. Presumably
the full version of add() pays attention to the format string -- but
thank you for stripping out that part of the code.)
You forgot to call va_end() in add().
The add() function requires a char* argument followed by zero or more
arguments of unknown types; since it invokes va_arg() with a type of
unsigned long, it will invoke undefined behavior unless you call it
with a char* and two unsigned longs. In set(), you call add() with a
char* and a va_list. The add() function probably tries to interpret
the va_list value as if it were an unsigned long, and then tries to
interpret whatever garbage happens to be where the third argument
*would* have been stored as if it were an unsigned long -- and that's
only with some optimistic assumptions. It's undefined behavior.
By analogy with the standard's printf() vs. vprintf(), you can create
a second function, say vadd(), that takes a format string and a
va_list. Your add() function could then be implemented as:
void add(char *format, ...)
{
va_list arg_ptr;
va_start(arg_ptr, format);
vadd(format, arg_ptr);
va_end(arg_ptr);
}
(I've only minimally tested a variant of this code; use with caution.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
thierrydollar wrote: I have written a very simple program using variable arguments calls and I get strange things that I cannot explain.
#include <stdio.h> #include <stdarg.h>
//--------------------------------------------------------------------------- void add (char *format, ...) { va_list arg_ptr; va_start (arg_ptr, format);
printf("format = %s\n",format); printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long)); printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long)); } //--------------------------------------------------------------------------- void set (char *format, ...) { va_list arg_ptr; va_start (arg_ptr, format); add(format, arg_ptr);
The function add expects the second and third argument to be of type
unsigned long, but you pass it a type arg_ptr for the first and nothing
for the second. The result is undefined behavior.
} //--------------------------------------------------------------------------- int main() { printf("\n>>>ADD \n"); add("H%dW%d",1234,5678);
Add expects two arguments of type unsigned long. You are providing two
arguments of type int. It may work on your system if int and long are
the same size, but is poor practice and not portable.
printf("\n>>>SET \n"); set("H%dW%d",1234,5678);
return 0; }
--
Thad
The problem I described is not related to the type of arguments. I had
also problems when passing strings.
thierrydollar wrote: The problem I described is not related to the type of arguments. I had also problems when passing strings.
We (usually) can't debug code we can't see.
--
Chris "understanding is a three-edged sword" Dollin
I have written another example of the problem with va_list
And here is the result
format = %s
arg[] = hello
format = %s
arg[] = äÂ*↕
Here is the source code
#include <stdio.h>
#include <stdarg.h>
//---------------------------------------------------------------------------
void add (char *format, ...)
{
va_list args;
va_start (args, format);
printf("format = %s\n",format);
printf("arg[] = %s\n", va_arg (args, char *));
}
//---------------------------------------------------------------------------
void set (char *format, ...)
{
va_list args;
va_start (args, format);
add(format, args);
}
//---------------------------------------------------------------------------
int main()
{
char txt[] = "hello";
add ("%s",txt);
set ("%s",txt);
return 0;
}
//---------------------------------------------------------------------------
"thierrydollar" <th*****@tetraedre.com> writes: #include <stdio.h> #include <stdarg.h>
//--------------------------------------------------------------------------- void add (char *format, ...) { va_list args; va_start (args, format);
printf("format = %s\n",format); printf("arg[] = %s\n", va_arg (args, char *)); }
you don't call va_end().
//--------------------------------------------------------------------------- void set (char *format, ...) { va_list args; va_start (args, format); add(format, args); }
you don't call va_end().
add() expects a char *, not a va_list.
DES
--
Dag-Erling Smørgrav - de*@des.no
On 2006-02-01, thierrydollar <th*****@tetraedre.com> wrote: The problem I described is not related to the type of arguments. I had also problems when passing strings.
Probably because you're also doing it wrong when you try to pass
strings.
"thierrydollar" <th*****@tetraedre.com> writes: The problem I described is not related to the type of arguments. I had also problems when passing strings.
What???
Please read <http://cfaj.freeshell.org/google/> *before* you post
another followup.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Groovy hepcat thierrydollar was jivin' on 31 Jan 2006 15:30:14 -0800
in comp.lang.c.
Trouble with va_list processing's a cool scene! Dig it! #include <stdio.h> #include <stdarg.h>
//--------------------------------------------------------------------------- void add (char *format, ...) { va_list arg_ptr; va_start (arg_ptr, format);
printf("format = %s\n",format); printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
%d format specifier with unsigned long argument.
printf("arg[] = %d\n", va_arg (arg_ptr, unsigned long));
%d format specifier with unsigned long argument.
No invokation of the va_end macro.
Assumes 2 unsigned long arguments will always be passed after
format. (Makes a varargs function unnecessary and undesirable.)
Not adding anything. (Makes the function name confusing.)
} //--------------------------------------------------------------------------- void set (char *format, ...) { va_list arg_ptr; va_start (arg_ptr, format); add(format, arg_ptr);
Passing a va_list to a function that is expecting two unsigned long
arguments.
No invokation of the va_end macro.
Not setting anything. (Makes the function name confusing.)
} //--------------------------------------------------------------------------- int main() { printf("\n>>>ADD \n"); add("H%dW%d",1234,5678);
Passing two ints to function expecting two unsigned longs.
printf("\n>>>SET \n"); set("H%dW%d",1234,5678);
return 0; }
If you want to pass a va_list to some function, you must have a
version of the function that takes a va_list. For example:
void vadd(const char *fmt, va_list ap)
{
/* Your code using va_arg(ap, some_type) goes here. */
}
Then your add() function becomes a wrapper around this, like so:
void add(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vadd(fmt, ap);
va_end(ap);
}
And your set() function is done similarly.
But you must make sure that a) you actually need varargs functions,
b) you don't make assumptions about the number and types of arguments
of varargs functions, c) when processing those arguments, you use them
correctly (eg., don't pass an unsigned long to printf() with a %d
conversion specifier - that's for signed int) and d) you don't pass
variadic arguments of the wrong type (eg., when the function expects
an unsigned long, and you want to pass 1234 to it, cast it to unsigned
long).
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
thierrydollar wrote: I have written another example of the problem with va_list
And here is the result
format = %s arg[] = hello format = %s arg[] = ä ↕
Here is the source code
#include <stdio.h> #include <stdarg.h>
//--------------------------------------------------------------------------- void add (char *format, ...) { va_list args; va_start (args, format);
printf("format = %s\n",format); printf("arg[] = %s\n", va_arg (args, char *)); } //--------------------------------------------------------------------------- void set (char *format, ...) { va_list args; va_start (args, format); add(format, args); } //--------------------------------------------------------------------------- int main() { char txt[] = "hello"; add ("%s",txt); set ("%s",txt);
return 0; } //---------------------------------------------------------------------------
Please read http://cfaj.freeshell.org/google/
Thank you!
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
thierrydollar wrote: The problem I described is not related to the type of arguments. I had also problems when passing strings.
Please read http://cfaj.freeshell.org/google/
Thank you!
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter |
last post by:
--------------------------------------------------------------------------------
Hi,
I was wondering if we can create a va_list by adding...
|
by: Douwe |
last post by:
I try to build my own version of printf which just passes all
arguments to the original printf. As long as I keep it with the single
argument...
|
by: Joerg Schoen |
last post by:
Hi folks!
I have a function that gets a 'va_list'. I am passing the
'va_list' two times to a function like 'vprintf' to print
it out.
I...
|
by: Alex |
last post by:
Can anyone point me to any good online references
about the va_arg() va_list() functions/macros
I googled and found a couple of things, but what...
|
by: Flash Gordon |
last post by:
Reading the standard, va_list is an object type (, so I believe the
following should be possible:
#include <stdarg.h>
void foo(va_list *arg)...
|
by: goldfita |
last post by:
My last post -
http://groups.google.com/group/comp.lang.c/browse_thread/thread/eacc2938b4c8ee66/0bdf6ab20a6007d0
I have a little bit more...
|
by: skillzero |
last post by:
Is there a portable way to pass a va_list as a parameter to another
function taking a variable argument list?
I have a function that takes a...
|
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= |
last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote:
The relevant paragraph from the Standard is:
---- Begin Quote ----
The type declared...
|
by: Chuck Chopp |
last post by:
I have some code that is being built on the following:
Windows Server 2003, both 32-bit & 64-bit editions
Windows Vista, both 32-bit & 64-bit...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |