0

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.

VaSaKed
  • 33
  • 5
  • Duplicate of [What exactly is va\_end for? Is it always necessary to call it?](http://stackoverflow.com/questions/587128/what-exactly-is-va-end-for-is-it-always-necessary-to-call-it) – user207421 Sep 19 '13 at 01:24

2 Answers2

4

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

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 and va_arg macros described in this subclause shall be implemented as macros, not functions. It is unspecified whether va_copy and va_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 the va_start and va_copy macros shall be matched by a corresponding invocation of the va_end macro in the same function.

It's the same in C++.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294