Memory

  • Note

    Computer block diagram

    Program execution

    Areas in memory

    Working

    As you can see on the left-hand side, we have a program on the hard disk. If we want to run this program, then this program i.e. the machine code of the program, first should be brought inside the main memory i.e. brought inside the code section of the Main memory. The area where the machine code of the program is reside called as Code Section of the Main memory. Once the machine code is loaded in the Code Section, then the CPU will start executing the program, and the program then will utilize the remaining memory i.e. stack and heap.

  • Examples STACK

    Example 1

    
                        void main()
                        {
                              int a;
                              float b;
                        }
    
                        

    Example 2

    
    
                        void fun2(int i)
                        {
                            int a;
                           
                        }
    
                        void fun1()
                        {
                              int x;
                              fun2();
                        }
    
                        void main()
                        {
                            int a;
                            float b;
                           
                            fun1();
                           
                        }
    
                        

    Now, we’ll see how the memory is allocated inside the stack for the above sequence of function calls. First of all, when we run the program, the machine code of the program will be copied in the code section of the Main Memory. When the program starts executing, it will start execution from the main function. The moment it enters inside the main function, it requires a variable. So, the memory for ‘a’ and ‘b’ will be allocated inside the stack area and that section is called the stack frame of the main method or Activation Record of the Main function.

    Next, the main function calls function fun1(). That means the control goes inside fun1(). Once the control inside the function fun1, the first thing it required is the variable. And the variable x is created inside the stack and this section is called Stackframe of fun1 function. Now, which function is executing? Currently, fun1() is executing because we have called it & the topmost activation record belongs to which function? Currently executing a function that is fun1().

    Then function fun1() calls function fun2(). Once the function fun2 is called, the control goes to fun2(). The function fun2() is having two variables, one is its parameter and the other one is its local variable.

    So, the memory is allocated for these variables “i” and “a” inside the stack, and this section is called the Stack Frame of the fun2 function. Now, presently fun2() is running and the topmost activation record inside the stack area is fun2(). For a better understanding of the above-discussed points please have a look at the following image.

    One thing that we need to be observed is that we started from the main function and it has not yet finished but it has called fun1(). So, the main function activation record is as it is inside the stack and then the activation record for fun1() is created i.e. memory for fun1() is allocated. then it is still running but it has called function fun2(), So, the activation record for fun2() is created and the activation record of function fun1() is still there in the memory.

    Now, let us continue our executing function, when fun2() has finished & terminated, then the control goes back to fun1(). Then what happens to the active record of fun2? This will be deleted. So, once the function fun2 completes its execution and once the control goes back to fun1, then the Action Record or the Stack Frame of the Fun2 function will be deleted from the Stack as shown in the below image.

    Now, let us continue our executing function, when fun1() has finished, then the control again goes back to the Main function and the Action Record or the Stack Frame of Fun2 function will be deleted from the Stack as shown in the below image.

    Now when the main method completes its execution, its Action Record will also be deleted from the Stack section of the main memory as shown in the below image.

  • Examples HEAP

    Array

    Example

    Object

    Example