Return_twice function attribute

I just looked for funciton attributes for gcc ( http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html ) and came across the returns_twice attribute.

And I absolutely do not know in which case the function can return twice ... I quickly looked at the mentioned vfork() and setjmp() , but I continue without an idea what the applicable scenario looks like: did any of you use it or can explain a little ?

+6
source share
2 answers

The setjmp function is similar to creating a label (in the sense of goto ), so you will first return from setjmp when you set the label, and then every time you really jump to it.

If this seems strange, be sure you should not use setjmp in your daily programming. Or actually ... you probably shouldn't use it at all. This is a very low-level command that splits the expected thread of execution (like t21) and, especially in C ++, most of the invariants you might expect.

+4
source

When you call setjmp , it sets that as the return point, execution continues in the code immediately after calling setjmp .

At some point in the code, calling longjmp (with the jump buffer initialized by the previous call to setjmp ) returns execution to start again from the same point (i.e., the code immediately after calling setjmp ).

Therefore, the original call returns normally, and then at arbitrary later points, execution returns (or, at least, may return) to the same point again.

The attribute simply warns the compiler of this fact.

+1
source

Source: https://habr.com/ru/post/950496/


All Articles