18.4. The Heap


The heap is an undifferentiated block of memory; an area of RAM in which your objects are created. When you declare an object (e.g., milo) you are actually declaring a reference to an object that will be located on the heap.

A reference is a variable that refers to another object. The reference acts like an alias for the object. When you allocate memory on the heap with the New operator, what you get back is a reference to the new object:

 Dim milo As New Dog(  ) 

Your new Dog object is on the heap. The reference milo refers to that object. The reference milo acts as an alias for that unnamed object, and you can treat milo as if it were the Dog object.

You can have more than one reference to the same object. This difference between creating value types and reference types is illustrated in Example 18-1. The complete analysis follows the output.

Example 18-1. Heap versus stack
 Module Module1    Public Class Dog       Public weight As Integer    End Class    Public Sub Main(  )       ' create an integer       Dim firstInt As Integer = 5       ' create a second integer       Dim secondInt As Integer = firstInt       ' display the two integers       Console.WriteLine( _          "firstInt: {0} second Integer: {1}", firstInt, secondInt)       ' modify the second integer       secondInt = 7       ' display the two integers       Console.WriteLine( _          "firstInt: {0} second Integer: {1}", firstInt, secondInt)       ' create a dog       Dim milo As New Dog(  )       ' assign a value to weight       milo.weight = 5       ' create a second reference to the dog       Dim fido As Dog = milo       ' display their values       Console.WriteLine( _         "Milo: {0}, fido: {1}", milo.weight, fido.weight)       ' assign a new weight to the second reference       fido.weight = 7       ' display the two values       Console.WriteLine( _         "Milo: {0}, fido: {1}", milo.weight, fido.weight)    End Sub End Module Output: firstInt: 5 second Integer: 5 firstInt: 5 second Integer: 7 Milo: 5, fido: 5 Milo: 7, fido: 7 

Main begins by creating an integer, firstInt, and initializing it with the value 5. The second integer, secondInt, is then created and initialized with the value in firstInt. Their values are displayed.

 firstInt: 5 second Integer: 5 

Because Integer is a value type, a copy of the value is made, and secondInt is an independent second variable, as illustrated in Figure 18-1.

Figure 18-1. A copy of firstInt


When you assign a new value to secondInt:

 secondInt = 7 

the first variable is unaffected. Only the copy is changed, as illustrated in Figure 18-2.

Figure 18-2. Only the copy is changed


The values are displayed and they are now different:

 firstInt: 5 second Integer: 7 

Your next step is to create a simple Dog class with only one member: a Public variable weight.

Generally, you will not make member variables Public, as explained below. The field weight was made Public to simplify this example.


You instantiate a Dog, and save a reference to that Dog in the reference milo:

 Dim milo As New Dog(  ) 

You assign the value 5 to milo's weight field:

 milo.weight = 5 

You commonly say that you've set milo's weight to 5, but actually you've set the weight of the unnamed object on the heap to which milo refers, as shown in Figure 18-3.

Figure 18-3. milo is a reference


You next create a new reference to Dog (fido) and initialize it by setting it equal to milo. This creates a new reference to the same object on the heap.

 Dim fido As Dog = milo 

Notice that this is syntactically similar to creating a second Integer variable and initializing it with an existing Integer, as you did before:

 Dim secondInt As Integer = firstInt Dim fido As Dog = milo 

The difference is that Dog is a reference type, so fido is not a copy of milo, it is a copy of the reference. Thus, fido is a second reference to the same object to which milo refers. That is, you now have an object on the heap with two references to it, as illustrated in Figure 18-4.

When you change the weight of that object through the fido reference, you are changing the weight of the same object to which milo refers. This is reflected in the output:

 milo: 7, fido: 7 

It isn't that fido is changing milo, it is that fido is changing the same (unnamed) object on the heap to which milo refers.

Figure 18-4. fido is also a reference




Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net