Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens under the hood when one Method Calls Another?

This is similar to What happens when you run a program?, but not a dupe.

Let's say I have a simple console program with two methods Aand B.

    public static void RunSnippet()
    {
        TestClass t = new TestClass();
        t.A(1, 2);

        t.B(3, 4);
    }

    public class TestClass
    {
        public void A(int param1, int param2)
        {
            //do something
            C();
        }

        private void C()
        {
            //do
        }

        public bool B(int param1, int param2)
        {
            //do something
            bool result = true;

            return result;
        }
    }

Can someone explain in detail (but please keep it in simple plain English), what really happens when RunSnippet calls method A and method B (and they internally call some other methods). I want to understand what really happens under the hood...meaning how are params passed, where are they stored, what happens to local vars, how are return values passed, What will happen if another thread starts running when A has called C, what will happen if an exception is thrown.

like image 578
Sandbox Avatar asked Dec 03 '22 07:12

Sandbox


1 Answers

I'm not quite sure what level of detail you're looking for, but here's my stab at explaining what's happening:

  1. A new process is created for your executable. That process has a stack segment containing each thread's stack, a data segment for static variables as well as a memory block called the heap for dynamically allocated memory, and a code segment containing the compiled code.
  2. Your code is loaded into the code segment, the instruction pointer is set to the first instruction in your main() method, and the code begins executing.
  3. Object t is allocated from the heap. The address of t is stored on the stack (each thread has it's own stack).
  4. t.A() is called by placing the return address to main() on the stack and changing the instruction pointer to the start of t.A()'s code. The return address is placed on the stack along with the values 1 and 2.
  5. t.A() calls t.C() by placing the return address to t.A() on the stack and changing the instruction pointer to the start address of t.C()'s code.
  6. t.C() returns by popping the return address to t.A() off of the stack and setting the instruction pointer to that value.
  7. t.A() returns in a similar manner to t.C().
  8. The call to t.B() is very similar to the call to t.A() except that it returns a value. The exact mechanism to return that value is language and platform dependent. Often the value will be returned in a CPU register.

Note: Since your methods are very small, modern compilers will often "inline" them instead of making a classic call. Inlining means taking the code from the methods and injecting them straight into the main() method rather than going through the (slight) overhead of making a function call.

Given your example I don't see how threading could come into the picture directly. If you were to start the executable a second time, it would run in a new process. That means it would get it's own code segment, data segment and stack segment completely isolating it from the first process.

If your code were run inside a larger program that called main() on several threads, it would run almost exactly as previously described. The code is thread safe because it doesn't access any potentially shared resources such as static variables. There is no way that Thread 1 could "see" Thread 2 because all key data (values and pointers to objects) is stored on the thread's local stack.

like image 156
Eric J. Avatar answered Dec 23 '22 07:12

Eric J.