Search code examples
c++stack-tracegoto

Stack Trace in Goto Statements


Suppose I use a goto statement in a recursive call, which redirects to my main function, what will happen to the information present in the stack memory? Suppose it is something like this,

void recurse(int n){
    if(n==0) goto p;
    ....
}
int main(){
    recurse(100);
    p:;
}

EDIT: As pointed out in the answer, goto statements cannot exactly be used outside the scope of the function, so the primary cause of doubt is wrong. Thanks for the help.


Solution

  • You can't, because goto can only go to a label in the same function.

    C++11 6.6.4 The goto statement

    The goto statement unconditionally transfers control to the statement labeled by the identifier. The identifier shall be a label (6.1) located in the current function.