How to implement stdarg in C

前端 未结 4 1320
無奈伤痛
無奈伤痛 2020-12-21 00:43

For curiosity, I\'m looking to write minimal replacements for some of the functions in the standard C library. So far, I have finished printf(), strlen()<

4条回答
  •  攒了一身酷
    2020-12-21 01:23

    On 32-bit x86 with the cdecl calling convention, parameters are passed on the stack:

    ^ higher addresses (lower on the stack)
    |
    | caller local variables
    | ...
    | argument 3
    | argument 2
    | argument 1
    | return address
    | saved EBP (usually)
    | callee local variables
    |
    v lower addresses (higher on the stack)
    

    You can implement va_list as a pointer. va_start can take the address of the argument passed to it and add the size of that argument to move to the next argument. va_arg can access the pointer and bump it to the next argument. va_copy can just copy the pointer value. va_end doesn’t need to do anything.

    If, on the other hand, you’re not using cdecl (maybe you’re using fastcall), you’re not 32-bit, you’re not x86, this won’t work; you might need to muck with registers rather than just pointer values. And even still it’s not guaranteed to work, since you’re relying on undefined behavior; as as example of only one potential problem, inlining could ruin everything. And that’s why the header file just typedefs it to a compiler built-in—implementing this in C is hopeless, you need compiler support. And don’t even get me started on implementing setjmp and longjmp

提交回复
热议问题