Trouble with va_list processing

T

thierrydollar

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

format = H%dW%d
arg[] = 1234
arg[] = 5678
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;
}
 
K

Keith Thompson

thierrydollar said:
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]
format = H%dW%d
arg[] = 1234
arg[] = 5678
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.)
 
T

Thad Smith

thierrydollar said:
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.
 
T

thierrydollar

The problem I described is not related to the type of arguments. I had
also problems when passing strings.
 
C

Chris Dollin

thierrydollar said:
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.
 
T

thierrydollar

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

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

thierrydollar said:
#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
 
J

Jordan Abel

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.
 
P

Peter Shaggy Haywood

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"?
 
N

Nelu

thierrydollar said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top