Search code examples
.netheap-memorydynamic-memory-allocationstack-memory

Where the function pointer is allocated when its begin executed from an class object?


I have read some articles and blogs about Stack and Heap. Now, I am reading this article for more information about memory allocation. The author says.

What happens when a method calls?

some 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.

Now I am confused here it says some memory get allocated for the information of function call in a stack. But, what happen when that function is declared in a class? and being executed by that class object

public class MyClass()
{
    public void publicFunc()
    {
        privateFunc();
    }

    private void privateFunc()
    {
        //some Code
    }
}


MyClass cls = new MyClass();
cls.MyFunction();

Now, I want to know that if the object of MyClass is stored in the heap storage then where the publicFunc() and privateFunc() function pointers will be stored? In the stack or in the same heap where the object of MyClass is allocated?


Solution

  • Remember that classes have the definition for the class that is separate from instances of the class. We can see that in this line of code:

    MyClass cls = new MyClass();
    

    That code causes three things to happen: a new cls variable is created of type MyClass. But cls is at it's core just a pointer, and so a new MyClass object is also created on the heap. Finally, the cls variable is set to refer to the new object. It's worth noting here that the MyClass object as shown isn't much more than a shell... there are no data members other than what is inherited from the base Object type. The MyClass type, though, is different from the object that was just created.

    The MyClass type does have some function members. Like classes, there is a sort of distinction between a function definition and one call into a specific function. Function definitions are not part of object instances; they are part of the compiled program... part of the program's memory, which is allocated on the stack when the program first starts up. There is no new memory that is allocated for the functions themselves when an object is created or when the functions are called.

    What does happen is the program will push a new entry onto the call stack indicating the function was called. This is different than allocating memory for the function itself; again, it is analogous to the difference between a type/class and an instance of a type/class. The call stack record will contain space for any stack variables used by the function. If, say, the privateFunc() function declared an integer as part of the function, there would be space in that stack frame for the integer. If it declared a class variable, there would be space on the stack frame for a class variable... but not the object referred to by the variable; it would just put space on the stack to hold a "pointer" that refers to whatever class type is used for the variable.

    At the end of the function, the program pulls the entry from the stack to see where to jump back to, so that program execution can continue from where it left off before the function was called. When there are no more entries on the stack, the program will exit.