Why does the caller have to clear the stack in the cdecl calling convention?

assembly, c, calling-convention, compiler-construction, stack

Solution

Because, with the C calling convention, the called function will not pop the parameters. That's the point of this calling convention.

It allows things like variable arguments.

Problem

From: http://en.wikipedia.org/wiki/X86_calling_conventions ``` push c push b push a call function_name add esp, 12 ;Stack clearing mov x, eax ``` Why do we need to explicitly add 12 to ESP to clear the stack since the called function should have poped the parameters off the stack therefore restoring the stack pointer...? Another question: Theoretically, it would be possible to implement variable parameter functions with the callee taking care of the cleanup right (for instance if you pass the number of arguments on the stack in a register)?

Original source