By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,144 Members | 872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,144 IT Pros & Developers. It's quick & easy.

Trouble with va_list processing

P: n/a
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;
}

Feb 1 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"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.
Feb 1 '06 #2

P: n/a
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
Feb 1 '06 #3

P: n/a
The problem I described is not related to the type of arguments. I had
also problems when passing strings.

Feb 1 '06 #4

P: n/a
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
Feb 1 '06 #5

P: n/a

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;
}
//---------------------------------------------------------------------------

Feb 1 '06 #6

P: n/a
"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
Feb 1 '06 #7

P: n/a
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.
Feb 1 '06 #8

P: n/a
"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.
Feb 1 '06 #9

P: n/a
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"?
Feb 3 '06 #10

P: n/a
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...)
Feb 3 '06 #11

P: n/a
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...)
Feb 3 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.