Reputation: 33
It is said that a call to va_start()
must be followed by the call to va_end()
because va_start()
(always?) disturbs the stack.
Can anybody explain me how does a call to va_start()
modify the stack and how this modification helps to get the variadic arguments.
Upvotes: 0
Views: 275
Reputation: 122383
Yes, each call to va_start
must be matched by a va_end
. I don't think it's necessary to know the implementation detail.
C11 §7.16.1 Variable argument list access macros
The
va_start
andva_arg
macros described in this subclause shall be implemented as macros, not functions. It is unspecified whetherva_copy
andva_end
are macros or identifiers declared with external linkage. If a macro definition is suppressed in order to access an actual function, or a program defines an external identifier with the same name, the behavior is undefined. Each invocation of theva_start
andva_copy
macros shall be matched by a corresponding invocation of theva_end
macro in the same function.
It's the same in C++.
Upvotes: 1
Reputation: 310874
It may modify the stack. It may do anything, or nothing. You don't know. Whatever it may or may not do is undone by va_end()
. That's why you have to call it. What it actually does, if anything, depends on the compiler and the processor architecture. There isn't a single answer.
Upvotes: 4