What sense do these clobbered variable warnings make?

前端 未结 2 1162
萌比男神i
萌比男神i 2020-12-30 03:48

I have a function like this:

#include 
jmp_buf buf;
void func2(int g);
extern int some_global;
void func(int x)
{
    if (setjmp(buf))
               


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 04:14

    From man longjmp:

    The values of automatic variables are unspecified after a call to longjmp() if they meet all the following criteria:

       ·  they are local to the function that made the corresponding setjmp(3)
          call;
    
       ·  their  values  are  changed  between  the  calls  to  setjmp(3)  and
          longjmp(); and
    
       ·  they are not declared as volatile.
    

    As it happens, your x variable in the first example meets the criteria:

    • It is local to the function, as function parameters are just like local automatic variables.
    • Its value may be changed just after setjmp if some_global is true.
    • It is not volatile.

    So its value may be unspecified (clobbered).

    About why the second version does not emit the warning... no idea.

提交回复
热议问题