2

I thought that the function removes the parameters from the stack after it's done, but a function like printf removes a variable number of parameters from the stack when it's called.

How does it know how many parameters to remove from the stack? Is there a secret argument to specify how many arguments are passed?

Thanks

3 Answers 3

4

The C calling convention specifies that is the caller and not the callee the one responsible from popping the parameters from the stack. That's why functions with a variable argument list must be cdecl. So,

I thought that the function removes the parameters from the stack after it's done.

That's only true for certain calling conventions, it isn't true for the C calling convention.

How does it know how many parameters to remove from the stack? Is there a secret argument to specify how many arguments are passed?

It doesn't, and no there is no secret argument.

Sign up to request clarification or add additional context in comments.

5 Comments

Does that mean that variable argument functions must have some argument that determines the number of arguments to be accessed (like the format string in printf) ?
@dhgfdg dgdfgds: There is no way to know how many parameters have been passed otherwise, so yes a variable argument list almost always requires another parameter specifying its contents.
There is no such thing as "the C calling convention". Any calling convention that makes it possible to satisfy the requirements for observable function call behavior in a way that matches the abstract machine is a viable implementation.
@R..: And that is exactly what I mean by C calling convention, a calling convention that satisfies the requirements of C.
There's no requirement that such a calling convention involve the caller doing the popping (or even that "popping" exists). One trivial way it could be satisfied with the callee doing the popping is by passing a hidden argument that's the number of bytes used on the stack for arguments.
1

The caller function will clean the stack (in the correct calling convention). The compiler will generate the code for that. The compiler is the one knowing exactly how many arguments you passed on the arguments list, because, well, it compiled it..

Comments

0

The calling code cleans up the stack, and it's up to the called function to correctly determine that there is "enough" arguments have been passed for whatever it wants to do. This doesn't have to be an argument as such, it could be something like this:

int sum(int first, ...)
{
    int s = first;
    int v;
    va_list va;
    va_start(va, first);

    while (v = va_arg(va, int) != -1)
    {
         sum += v;
    }

    va_end(va);
    return sum;
 }



 x = sum(1, 2, 3, -1);
 y = sum(1, 2, 3, 4, 5, 6, 7, 8, 9, -1);        

Link to how many arguments in varargs function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.