Newbie Question about Value and Reference Types



Hi!

I'm new to vb.net (and to programming as well) and last night I tried
to make my own picture of the difference of value and reference types.


What I find out is that the main difference is how the CLR handles the
memory for this types. If a new instance of a value type is generated
then for this instance an address exist which points to a formatted
chunk of data on the stack. This adress which points on the stack can
be regarded as the name of the instance which is connected to the name
of the instance in the source code.


When ever the program execution leaves the scope of the instance the
used memory on the stack will immediately be reclaimed and the instance
no longer exists.


If a new instance of a reference type is generated then the same
happens with the stack with the difference that the data on the stack
is an adresse which points to the heap where the real data of the
instance is. If the instance variable is set to nothing or if the
instance is out of scope then the used memory on the stack is again
immediately reclaimed but the instance still lives due to the data on
the heap. First if the garbage collector decides to clean up instances
which no longer are reachable by the application then the instance will
be removed totally from the memory.


If an instance is passed to an implementation then one has to distinct
the four cases:


1) ByVal Value Type
2) ByVal Reference Type
3) ByRef Value Type
4) ByRef Reference Type


If an instance is passed by value semantics then a copy of the data on
the stack is made and the adress of this copy is passed to the
implementation while by reference semantics pass the adress of the
original data on the stack. If the instance is a value type then the
implementation has no access to the original instance at all as the
implementation only get the name (adress) of the copy on the stack.


But if the instance is a reference type then again the implementatoin
gets a adress of the copy on the stack but the data on the stack points
to the real data on the heap. So this way the implementation has access
to the data of original instance. What the implemantaion can do not is
to change the target of the reference of the instance. That means it
can not change the data on the stack of the original instance. The
pointer to the place to the heap can not be changed.


Last but not least one have the case where a reference type is passed
by reference. Then the implementation not only can modify the data on
the heap. As the implementation gets the original adress on the stack
where the location of the heap's date is found the implementation can
chage this value and therefore can the reference variable point to a
totally different place on the heap. That means the reference variable
can point to a different instance.


Correct?


If so then I have another question: If a reference type is passed by
reference to a procedure and within this procedure a new instance of
the same type is generated and assigned to the passed reference
variable what happens to the instacne on the heap if the program
execution leaves the procedure? I think the original instacne on the
heap is now in danger to be killed by the garbage collector as no
reference points to it anymore:


Sub Main()
....
Dim a As New Object
....
Test(a)
.....
End Sub
....
Sub Test(ByRef a As Object)
Dim b As New Object
a=b
End Sub


Correct?


GFM GToeroe


.



Relevant Pages

  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how java variables are stored?
    ... The reference to a local array can go on the ... >> stack but the array object itself goes on the heap. ... In the case of how the Java memory/variable/storage model works, ...
    (comp.lang.java.programmer)
  • Newbie Question
    ... to make my own picture of the difference of value and reference types. ... This adress which points on the stack can ... If a new instance of a reference type is generated then the same ... is an adresse which points to the heap where the real data of the ...
    (microsoft.public.vb.general.discussion)
  • Newbie Question about Value and Reference Types
    ... to make my own picture of the difference of value and reference types. ... This adress which points on the stack can ... If a new instance of a reference type is generated then the same ... is an adresse which points to the heap where the real data of the ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Boxing and Unboxing of Value-Types when passed to a method call
    ... After all, all method arguments _must_ be allocated on the stack, ... we also know that "b" is a direct reference to the memory ... The heap is not involved at all in this case. ... the compiler must create a temporary object to hold ...
    (microsoft.public.dotnet.languages.csharp)