Nikolai Kolev
Nikolai Kolev

Reputation: 19

What happens after an assignment to the reference and the memory?

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:

my thoughts

or this:

what it says in the book

and what exactly happens to the memory?

Upvotes: 1

Views: 116

Answers (3)

Flater
Flater

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

Sweeper
Sweeper

Reputation: 273125

I will tell exactly what happens, assuming T is a reference type.

Originally,

enter image description here

After first line:

enter image description here

After second line:

enter image description here

Basically what happens is:

  • obj1 and obj2 now refers to the same object.
  • The BinaryTree object originally referred to by obj1 becomes inaccessible.
  • an object of T (formerly known as obj1.value) becomes inaccessible.

Upvotes: 0

Guilherme Holtz
Guilherme Holtz

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

Related Questions