Is it safe to use va_list in exception-prone code?

后端 未结 3 2307
生来不讨喜
生来不讨喜 2021-02-20 01:32

Typical example:

void foo(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);

    // might throw, might not.  who knows.
    bar(fmt, args);

            


        
3条回答
  •  粉色の甜心
    2021-02-20 02:07

    As mentioned above it is Undefined behavior by the c standard. Yet depending on your platform the marco can compile into different things, I see for me for example it just does args = 0; And va_list is a char*; In which case it seems that the end macro isnt doing anything critical. Nothing bad should happen except im not sure who will deallocate the args, but i don't know where they are allocated in the first place.

    I don't in any way recommend using this but sometimes crazy things are necessary for supporting legacy code. If you can use a try catch there then by all means do so.

提交回复
热议问题