Reputation: 19
I came across a Remove()
function that doesn't seem right to me . We have 2 variables obj1
and obj2
. Both are from the same class.
public class BinayTree<T> (){
public T value;
public BinayTree<T> parent;
public BinayTree<T> leftChild;
public BinayTree<T> rightChild;
}
if we say
obj1.value = obj2.value;
obj1 = obj2;
would this happen:
or this:
and what exactly happens to the memory?
Upvotes: 1
Views: 116
Reputation: 13803
I hope the figures help you understand what exactly is happening.
Step 1 - Initializing the objects
var obj1 = new BinaryTree<int>();
obj1.Value = 123;
var obj2 = new BinartyTree<int>();
obj2.Value = 456;
We have created two objects in memory, each one assigned to a particular variable. For the sake of example, let's assume they are found in memory locations 333 and 555:
obj1 obj2
| |
| |
| |
V V
_____________ _____________
| #333 | | #555 |
|_____________| |_____________|
| | | |
| Value: 123 | | Value: 456 |
| | | |
|_____________| |_____________|
Step 2 - Changing a value
Here, we are changing the value of a property in the object that obj1
is referring to.
obj1.value = obj2.value;
The value of the Value
field in obj1
is changed:
obj1 obj2
| |
| |
| |
V V
_____________ _____________
| #333 | | #555 |
|_____________| |_____________|
| | | |
| Value: 456 | | Value: 456 |
| | | |
|_____________| |_____________|
Step 3 - Changing a reference
obj1 = obj2;
Here, we are changing the reference to the object that obj1
points to.
obj1 obj2
\__ |
\__ |
\__ |
\__ V
_____________ \__ _____________
| #333 | _\|| #555 |
|_____________| |_____________|
| | | |
| Value: 456 | | Value: 456 |
| | | |
|_____________| |_____________|
We did not change the value of any of the objects. We merely changed which object that obj1
points to.
Based on your question, I can see that you are expecting the code to implicitly link the two objects to each other.
Think of it this way: if your expectation was the case, then we wouldn't need classes like BinaryTree<T>
with inner references to other BinaryTree<T>
objects.
The existence of your example class proves that your expectations are not the case.
Edit
My example assumes that T
in BinaryTree<T>
is a value type. If this is a reference type (e.g. a custom class you created), then step 2 gets a lot hairier. I don't think I could adequately draw that with my limited ASCII art skills.
Without trying to offend, there is a clear discrepancy between your grasp on the basics of reference type assignment and the complexity of the example code that you posted.
You are currently struggling with the basics of reference type variables. I suggest to brush up on the basics before you try to tackle generic classes with reference type arguments; as this is going to get massively more complicated without understanding how reference type assignment works.
Upvotes: 1
Reputation: 273125
I will tell exactly what happens, assuming T
is a reference type.
Originally,
After first line:
After second line:
Basically what happens is:
obj1
and obj2
now refers to the same object.BinaryTree
object originally referred to by obj1
becomes inaccessible.T
(formerly known as obj1.value
) becomes inaccessible.Upvotes: 0
Reputation: 485
Since they are object instances, they are reference typed. And by the time you do this assignement:
obj1 = obj2;
The variable obj1
will reference the same object in obj2
. They are referencing the same object data in the memory.
The object that was referenced in obj1
before is now lost and will be cleaned in the memory by the garbage collector.
Upvotes: 0