I have a function f( ) in a file func.c and functions f1( ), f2( ), f3() , f4( ) in another file funcs.h. (Assume that all the functions receive/return values without any loss of generality).
- Function f( ) calls f4( )
- f4( ) calls f1( ), f2( ), f3( ) in some arbitrary order, among themselves
- At some point of time during the execution, f3() detects the completion of the algorithm and it has to "terminate" the execution of the algorithm. In a standalone case, it should exit out of the program after printing the solutions.But here, I need f3( ) to return to f( ).
This is my solution:
In this scenario, I cannot simply return to f4() (the original function called by f(), since there is already a function call stack of f1(), f2(), f3(),f4(), waiting to be "popped"). So, what I did is:
- I did a setjmp( ) in f() before calling f4( )
- And then, I did a longjmp( ) in f3( ) when I detected the completion of the algorithm
My question is: Is this the correct way to achieve this in this scenario?