How does the C compiler implement functions with Variable numbers of arguments?

前端 未结 4 1720
别那么骄傲
别那么骄傲 2021-01-02 00:17

I attended a technical interview a few days ago, and I was asked How does the C compiler implments function with Variable number of arguments? How does it pass the on the st

4条回答
  •  再見小時候
    2021-01-02 00:32

    It implements them using the va_ macros - for example va_start. Exactly what these macros do is implementation defined - in other words it will vary from CPU architecture to architecture, and from compiler to compiler. But they must play tricks with the C call stack. Typically, this will involve taking the address of the last named parameter as a base, and then accessing the variadic parameters by performing pointer arithmetic on this base.

提交回复
热议问题