tags:

views:

206

answers:

3

I do an object initialization as done in the code below:

namespace PatternPractice
{
 public class TestClass
 {
    private DecoratorClass decoratorClass;
    public TestClass()
    {

    }

    public void addTest()
    {
        decoratorClass = new DecoratorClass();
        testMethod(decoratorClass);
    }

    public void testMethod(DecoratorClass d)
    {
        Console.WriteLine("Am doing to explain my question.");
    }
   }
}

I have the following questions:

a. What is decoratorClass variable? A reference to the memory location where this variable is created, or something else?

b. In addTest() method I initialize this decoratorClass, what has this become now, a reference to the object, or something else?

c. In case this becomes a reference to the object, then why do we use ref in parameters we want to pass by reference. This question doesn't hold good if decoratorClass is not a reference and something else.

d. In testMethod() method I know that a copy of decoratorClass is created and passed, again is it a reference or something else?

I am a bit confused at how all this happens at runtime (ASP.NET or JVM etc.) end, basically related to object creation in Memory.

Is there any article which can explain how object creation happens by runtime and how they are allocated to memory.

Thanks for the help.

+1  A: 

a. If DecoratorClass is a class - then yes, it is a reference. If it is a struct, then class TestClass contains the whole instance of DecoratorClass.

b. See "a"

c. ref is used when you want to change the actual reference, not only object it is referencing

d. If DecoratorClass is class, then only reference to it is copied and passed to the testMethod. If struct - then the whole instance is copied and passed to testMethod

Vitaliy Liptchinsky
A: 

a. At the top of your class Decoratorclass is just a variable with no memory allocated. Objects are created on the Heap and not on the Stack.

b. In testmethod you have now allocated memory for this DecoratorClass object on the heap and your variable decoratorClass becomes a reference to that memory being allocated on the heap.

c. Ref parameters are mostly used to pass value types such as (int, long, char, etc). As answer above if you want to change what your object is referencing in memory, you can pass the object (Reference Type) using Ref. Now you change what that object references.

d. as answer above, only the reference is passed. So the reference to where this object is located is passed on the Heap and this you cannot change when passing it like this. If using Ref to pass it, you can change what it references. Now you can only change the data it contains.

Tony
Please correct me if am wrong: You want to say that in (a) an address/reference is created with no memory location being referred via it.In (d) when C# runtime executes it, it might be taking the object which decoratorClass reference points to --> then it copies object to some other location and then that reference is passed to the testMethod. And in case the ref keyword is used, it gives the same object (which decoratorClass points to) to the method.Let me know if my understanding is wrong.
Beginner
No, it passes by reference, but using the Ref keyword, you are able to change what it references, without the keyword, you cannot change what it references. But both are passing a reference.
Tony
+1  A: 

Basic concept of value type and reference type.

Any type of variable (whether it is value type or reference type), is pushed on the stack when it is declared.

However, the difference between them is, in case of value types, the value of the object gets stored on stack. Whereas, in case of reference types, the address of the heap gets stored on stack.

So consider the statements:

int a;
DecoratorClass decoratorClass;

Now when these statements run, this is the stack:

STACK:

Variable     Value
a           0   // Since ints are assigned to 0 by default
decoratorClass    NULL

Now if you try to access the decoratorClass e.g. decoratorClass.memberVariable = xyz; etc. then you will get a NullPointerException. This is because, decoratorClass is only declared not initialized. It will get initialized only when you call the constructor.

So when the below statement runs:

decoratorClass = new DecoratorClass();

1) Memory gets allocated on the heap at a particular location(address). The amount of memory allocated in bytes depends upon the class definition (member variables)

HEAP:

xEEEE00
xEEEE01
xEEEE02
xEEEE03

2) Now since some memory is allocated to the object the stack will be updated with the address of the memory

STACK:

    Variable     Value
    a           0   // Since ints are assigned to 0 by default
    decoratorClass  xEEEE00

Now if you try to access the decoratorClass.memberVariable, you will not get a NullPointer exception because the instance is initialized and memory location is assigned on the stack.

NOW ANSWERS TO YOUR QUESTIONS:

a. What is decoratorClass variable? A reference to the memory location where this variable is created, or something else? AND b. In addTest() method I initialize this decoratorClass, what has this become now, a reference to the object, or something else?

Ans: The decoratorClass variable will be pushed on to stack when the declaration statement is run. It's values on the stack (which is supposed to be the address on the heap) will be null as memory is not yet allocated on the heap. Memory will be allocated on the heap when the constructor is called. The stack will now contain the address on the heap

Let me answer d before I answer c, as I believe it would be simpler for you to understand that way.

d. In testMethod() method I know that a copy of decoratorClass is created and passed, again is it a reference or something else?

Ans: With the explanation I have given above, in the method testMethod(DecoratorClass d), d is a local variable to testMethod(). But what is the value in d (i.e. value on stack)? It is the address of decoratorClass. So the local variable d points to the same heap and hence the accessing the memberVariable of d will give the same value as accessing the memberVariable of decoratorClass.

Now if you create a new DecoratorClass and assign it to d, it WILL NOT CHANGE decoratorClass. So d might point to EEEE06, but decoratorClass will still point to EEEE00.

Now let's move to c.

c. In case this becomes a reference to the object, then why do we use ref in parameters we want to pass by reference. This question doesn't hold good if decoratorClass is not a reference and something else. Ans: Let's consider the same method testMethod(ref DecoratorClass d). Since you are passing d as ref, it is no longer a local variable, but its the same variable.

Now if you create a new DecoratorClass and assign it to d, it WILL CHANGE decoratorClass. So d will point to EEEE06, but will have the effect of decoratorClass also pointing to EEEE06 as both are the same variables on the stack.

For more details on pass object by ref v/s pass object by value check out Memory games

Hope I am elaborate enough.

Cheers!

Rashmi Pandit
Please could you post some links which can help me in getting the whole back ground cycle executed by runtime.
Beginner
http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx is a good MSDN link. However, once you understand the basic concepts, i.e. what (value types/ ref types) is stored where (stack/ heap) and when (declaration/ initialization) all the pieces will automatically fall in place.
Rashmi Pandit
Thanks Rashmi.Answer for (a), (b), (c) are perfect. For (d) there is something a miss, it is:Consider "A" is the object on Heap pointed by "decoratorClass" variable, now "d" is a local variable in the testMethod i.e. a new reference (in case this is not passed by ref otherwise it is same) on stack which points to the object passed to this method and, in case this object is passed by ref then it is "A" otherwise it is a "copy of A" say "A1", both copies at heap. In your answer to (d):"So the local variable d points to the same heap and hence the accessing" Will it be same heap then?
Beginner
In my above question my concern is that, there will be another copy of "decoratorClass object" created on Heap, this copy is to be pointed by localVariable "d" passed in the method testMethod and another copy will be pointed by decoratorClass variable.In case the heap is same then ref passing and passing without ref will have the only difference that the variables are same as answered by you in (c), since the heap will be the same in both cases.
Beginner
Check out http://rashmipandit.wordpress.com/2009/12/01/memory-games/
Rashmi Pandit
Hi Rashmi, Explanation In TestMethod1() is a bit confusing to me.STACKVariable Value————– ——-originalSample x000000a x000000Heap location needs to be changed as it creates a copy when passing by value. "a" can be x000050 but the object in it will be a shallow copy of the object which is at x000000.http://stackoverflow.com/questions/410593/pass-by-reference-value-in-chttp://stackoverflow.com/questions/373419/whats-the-difference-between-a-parameter-passed-by-reference-vs-passed-by-value#373429
Beginner
Shallow copy means only the reference to the object is copied, not the object itself. Thus, originalString is the reference to the object on the heap. Hence it is copied to a which becomes a reference to the same heap address. Deep copy means the copy of the object is created on the heap which is in the case of TestMethod2(). Creates a copy when passing by value means -- create a copy of the reference i.e. copy of the stack and not the heap.
Rashmi Pandit
Think of it as this way --- Pass by value ... a copy 'a' is created on the stack of originalString and a is passed. Pass by reference ... copy of originalString is not created, a reference to originalString is passed in 'a'. If we would have been dealing with value types i.e. pass an Int32 by ref or value, it would have been simple to understand. Passing a class across is making it confusing.
Rashmi Pandit
I think you are right. I read about it and tested the code written at: http://msdn.microsoft.com/en-us/library/s6938f28%28VS.80%29.aspxAnd this cleared my doubts and is same what you said.Thanks a lot for the help.
Beginner
I am glad your doubts are finally cleared :D ... Cheers!!
Rashmi Pandit