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

Correct use of va_copy

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Johan Lindh wrote:
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.
Nov 14 '05 #2

P: n/a
Jeremy Yallop wrote:
Johan Lindh wrote:
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.


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

Nov 14 '05 #3

P: n/a
Johan Lindh <jo***@linkdata.getridofthis.se> wrote:
Jeremy Yallop wrote:
Johan Lindh wrote:
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.


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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #4

P: n/a
Irrwahn Grausewitz wrote:
Johan Lindh <jo***@linkdata.getridofthis.se> wrote:
Jeremy Yallop wrote:
Johan Lindh wrote:

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.
I only have the C99; if it's needful to know the n869 (whatever THAT
is... =), please post it.


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.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.