Correct use of va_copy

J

Johan Lindh

Hi all,

If I read the standard correctly, a va_list variable
that's initialized using va_copy() should be terminated
with a va_end() on it. Is this correct?

int test( va_list x )
{
va_list x_copy;

va_copy( x_copy, x );
va_end( x_copy );

return 0;
}


Regards,

Johan
 
J

Jeremy Yallop

Johan said:
If I read the standard correctly, a va_list variable
that's initialized using va_copy() should be terminated
with a va_end() on it. Is this correct?

Yes. Note that the relevant text has changed between n869 and C99.

Jeremy.
 
J

Johan Lindh

Jeremy said:
Yes. Note that the relevant text has changed between n869 and C99.

I only have the C99; if it's needful to know the n869 (whatever THAT
is... =), please post it.
 
I

Irrwahn Grausewitz

Johan Lindh said:
I only have the C99; if it's needful to know the n869 (whatever THAT
is... =), please post it.

N869 is the last public draft of the ISO/IEC 9899:1999 standard
and is available at:
http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/.


n869 7.15.1.3 (The va_end macro):
[...]
[#2] The va_end macro facilitates a normal return from the
function whose variable argument list was referred to by
the expansion of va_start that initialized the va_list ap.
The va_end macro may modify ap so that it is no longer
usable (without an intervening invocation of va_start).
If there is no corresponding invocation of the va_start
macro, or if the va_end macro is not invoked before
the return, the behavior is undefined.

ISO/IEC 9899:1999(E) 7.15.1.3 (The va_end macro):
[...]
2 The va_end macro facilitates a normal return from the function
whose variable argument list was referred to by the expansion
of the va_start macro, or the function containing the expansion
of the va_copy macro, that initialized the va_list ap. The
va_end macro may modify ap so that it is no longer usable
(without being reinitialized by the va_start or va_copy macro).
If there is no corresponding invocation of the va_start or
va_copy macro, or if the va_end macro is not invoked before the
return, the behavior is undefined.

HTH
Regards
 
J

Jeremy Yallop

It's not needful. n869 is an obsolete draft of the standard. I only
mentioned it seems to be fairly widely used here, and it gives
misleading information in this case.
n869 7.15.1.3 (The va_end macro): [...]
ISO/IEC 9899:1999(E) 7.15.1.3 (The va_end macro):
[...]

FWIW, I was referring to 7.15.1, specifically

Each invocation of the va_start and va_copy macros shall be matched
by a corresponding invocation of the va_end macro in the same
function.

n869 says:

Each invocation of the va_start or va_copy macros shall be matched
by a corresponding invocation of the va_end macro in the function
accepting a varying number of arguments.

which is wrong, particularly with respect to the OP's code, where
va_copy was invoked in a function that accepted a single argument.

Jeremy.
 

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

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top