user2147163
user2147163

Reputation: 87

Value type and Reference type real time memory allocatoin

I have recently been asked in an interview like where actually variables are stored. I answered value type variables are stored on Stack and Reference type variables are stored on Heap but the interviewer asked me to give me real time scenario in a program.Please check the below program and help me where value types are stored and who are going to store in Reference type.If you can give example with program it will be very helpful or let me know any tool where I can check in VS where these are getting stored

class Program
{
    int i = 5;
    bool a;
    string str = "Stack Overflow";
    object obj = "object";
    string employeeName;
    int employeeCode;

    public int Add(int x,int y)
    {
        float a = 7.0F;
        return x + y;
    }


    static void Main(string[] args)
    {
        Program obj = new Program();
        obj.employeeCode = 1000;
        obj.employeeName = "John";
        int result = obj.Add(10, 15);
    }
}

Please let me know where these variable are stored any why .Give some example of value type and reference type both

Upvotes: 1

Views: 107

Answers (1)

Sloven
Sloven

Reputation: 335

The following is the overview of what's going on under the hood. The best examples I could provide is a series of figures with Stack and Heap representation.

First of all, let's imagine that we have 2 classes: Soldier and Officer. Officer extends Soldier. Also, we have some logic inside method1. When the execution of method1 begins we have some state of stack and heap. Stack has something from previous execution which is not important right now and Heap has our classes definitions.

Initial state

1) At the beginning of method1, we have a couple of declaration statements so two variables will be pushed into stack s and age. Right now s has nothing to point to and age == 0.

enter image description here

2) s = new Officer() statement creates new object in Heap and address of this object is now assigned to s

enter image description here

3) Let's imagine that Soldier.Lookup(name) is a static method in Soldier class. For example, it queries Database and generates new Officer object. As a result, the s pointer will point to a new object and the previous object is a candidate for garbage collection.

Static method call

4) The next statement is just a value assignment. age is now 31 (31 is the result of GetSoldierAge() operation).

Simple assignment

5) s.CurrentStatusReport() has some side effects (print report) and does not affect our memory, so we'll just ignore it. The next statement is a call of sendOutForTraining(age, s) method and here is a lot of fun :)

Value/Reference. The interesting part.

6) When execution comes into sendOutForTraining(age, s) the following happens:

  • pointer to sendOutForTraining is pushed onto the stack (for returning purposes).

  • Variable a is created and pushed onto the stack. The value from age is copied into the a right away.

  • Variable sldr is created and pushed onto the stack. The pointer from s is copied into sldr and now s and sldr are pointing to the same object.

entering sendOutForTraining

entering sendOutForTraining

7) The result of a += 2 is computed and stored in the stack. As you can see, the age and a are different variables and contains different values.

a assignment

8) Alright, sldr.age = a . It should be clear that the result of this operation will affect the shared object that s and sldr are pointing. So the age should be equal 33 outside of this method.

age changing

9) Another assignment. sldr = new Soldier(); The result is a brand new object and its address is now stored inside sldr. The s variable from outside of the method don't know about it and absolutely don't care.

New object created

10) The final statement simply will change the instance field of the object under the sldr address.

Change age of another object

11) The method execution ends, every artifact should be cleaned when the job is done. Unused variables will be destroyed and objects will be garbage collected.

Cleaning artifacts

12) As we can see, sedOutForTraining has lived the mark. The instance variable of Officer object has it's age = 33.

The final state


Upvotes: 3

Related Questions