Shivanand Arur

Shivanand Arur

  • NA
  • 3.6k
  • 1.4m

What happens in the memory when a method is executed.. and when we pass some parameters to that method

Jul 9 2011 7:58 AM

I have been trying to learn about the memory management in C# and it is a very interesting topic. I made one simple code which is as follows:

using System;  public class MyClass {     public void Go()     {       int x = 5;       AddFive(x);     }      public int AddFive(int pValue)     {       pValue += 5;       Console.WriteLine(pValue);       return pValue;     }      public static void Main(String[] args)     {       MyClass mc1 = new MyClass();       mc1.Go();       Console.ReadLine();     }  }  

I was reading an article from this website about the memory management by Matthew Cochran and it had the following statements: 

When we make a method call here's what happens:

  1. Space is allocated for information needed for the execution of our method on the stack (called a Stack Frame). This includes the calling address (a pointer) which is basically a GOTO instruction so when the thread finishes running our method it knows where to go back to in order to continue execution. 
  2. Our method parameters are copied over. This is what we want to look at more closely.
  3. Control is passed to the JIT'ted method and the thread starts executing code. Hence, we have another method represented by a stack frame on the "call stack".

Please explain me all the three points(especially the first and the third point) in a very simple way. I am a beginner and i want to know what happens when we execute a method..... Thanks in advance!!!

 


Answers (2)