VaSaKed
VaSaKed

Reputation: 33

va_start() modifies stack

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

Answers (2)

Yu Hao
Yu Hao

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

Upvotes: 1

user207421
user207421

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

Related Questions