How does the compiler know where control should return after a function call?

Consider the following functions:

int main()
{
    //statement(s);
    func1();
    //statement(s);
}

void func1()
{
    //statement(s);
    func2();
    //statement(s);
}

void func2()
{
    //statement(s);
}

How does the compiler know where to return after having func2completed all its operations? I know that control passes functions func1(and exactly which operator), but how does the compiler know about this? What tells the compiler where to go back?

+5
source share
4 answers

This is usually done using the call stack :

  • When control is passed to the function, the address to be returned is pushed onto the stack.
  • When the function completes, the address is unloaded from the stack and used to transfer control back to the called party.

, , , .

+15

, , , , , , , , (IP) .

.

+3

, -, , , , .

, , - (, ), , .

+2

, " ", . , , , , . . ? , , " " , - , . , Visual Studio - " ", , .

+1
source

All Articles