A stack/heap question

Z

ZorpiedoMan

When you create a new object, when is it added to the heap? More
specifically:

Sub go(ClassRef as myClass)

Dim myObj as new myClass = ClassRef

End Sub


does the NEW directive create the reference on the stack and an
empty/initialized instance on the heap, THEN change the reference to the
ClassRef object? Or does it bypass the extra step and immediately just
reference the already existing ClassRef object?
 
T

Tom Shelton

When you create a new object, when is it added to the heap? More
specifically:

Sub go(ClassRef as myClass)

Dim myObj as new myClass = ClassRef

End Sub


does the NEW directive create the reference on the stack and an
empty/initialized instance on the heap, THEN change the reference to the
ClassRef object? Or does it bypass the extra step and immediately just
reference the already existing ClassRef object?

That won't even compile. Which makes sense, because you would simply be
allocating a new object only to orphan it by assigning a new reference
to the variable. I suppose the compiler might be able to be made smart
enough to recognize that - but instead it is a syntax error.
 
R

Rajesh Patel

is DIM statement working? I doubt.

if it works. it will work like this.

dim myObj as new myclass
myobj = classref

you are creating one new instance of myclass other than classref.
and the pointer myobj points to new class.
but after myobj = classref
pointer is going to set to the classref and
your new instance will remain unpointed. i.e. nobody can reach that instance
after that. it will be deleted by garbage collector.

Rajesh Patel
 
A

Armin Zingler

ZorpiedoMan said:
When you create a new object, when is it added to the heap? More
specifically:

Sub go(ClassRef as myClass)

Dim myObj as new myClass = ClassRef

End Sub

does the NEW directive create the reference on the stack and an
empty/initialized instance on the heap, THEN change the reference to
the ClassRef object? Or does it bypass the extra step and
immediately just reference the already existing ClassRef object?


This syntax does not work. It doesn't make sense because the new keyword
creates a new object and assigns it to myObj. Additonally assigning ClassRef
to the same variable does not make sense because only one reference can be
stored in a variable.
 
J

Jay B. Harlow [MVP - Outlook]

ZorpiedoMan,
When you create a new object, when is it added to the heap?
A new object is added to the heap as soon as it is created. (by the time
your constructor (Sub New) is executed).
Dim myObj as new myClass = ClassRef
Fortunately!! VS.NET 2003 gives you a compile error, I have not checked
VS.NET 2002, I would hope VS.NET 2002 also gives a compile error!

The above is invalid syntax as you are attempting to initialize the myObj
variable with two distinct values (classref & a new myclass).

Use one or the other.

Hope this helps
Jay
 
C

Cor

Hi Jay and Tom

I think the OP ask something what if I do this and maybe did he made a
syntax error.
(Of course I can see this wrong)

Public Class Class1
Public Sub New()
Dim newclass As New Class1
End Sub
End Class

A kind of code I love to do (conditional of course).

:))

I never am very intrested what happened than, because this is for me nothing
more than a stack operation and therefore everytime new generated, it would
give strange results if it was not.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
I read the OP's question to be what Rajesh stated.

dim myObj as new myclass
myobj = classref

Only stated as a single statement as the OP demonstrated.

However only the OP knows what the OP was asking ;-)

Your code of course will cause all sorts of problems without a condition,
not sure if you will run out of memory before the stack overflows... ;-)

Jay
 
Z

ZorpiedoMan

Correct. Syntax error on my part. Rajesh seems to have given me the answer
I was looing for. Dim as New creates a new object on the heap which is
ignored when you set the stack to point to the existing object and cleand up
on the next GC.

so what happens if is correctly written:

Sub go(ClassRef as myClass)

Dim myObj as myClass = ClassRef

End Sub

In this case does it still create a new, empty class on the heap or does it
skip this step now?

-zm
 
J

Jay B. Harlow [MVP - Outlook]

ZorpiedoMan,
In this case does it still create a new, empty class on the heap or does it
skip this step now?
No.

Remember as you stated "Dim as New creates a new object on the heap".
Dim myObj as myClass = ClassRef

You are initializing the myObj field to the value of the ClassRef field
(parameter).

If you have:
Dim myObj as myClass
The myObj field is implicitly initialized with the value of Nothing, no
objects are created, this can be rewritten as:
Dim myObj as myClass = Nothing

Which explicitly initializes myObj to Nothing.

To create an object you need to use the New keyword, such as:
Dim myObj as New myClass

Or alternatively (more verbosely)
Dim myObj as myClass = New MyClass

If you want to see what is happening, add a constructor (Sub New) to your
myClass class and single step the code. As the constructor will be called
whenever a new object is created.

Hope this helps
Jay


ZorpiedoMan said:
Correct. Syntax error on my part. Rajesh seems to have given me the answer
I was looing for. Dim as New creates a new object on the heap which is
ignored when you set the stack to point to the existing object and cleand up
on the next GC.

so what happens if is correctly written:

Sub go(ClassRef as myClass)

Dim myObj as myClass = ClassRef

End Sub

In this case does it still create a new, empty class on the heap or does it
skip this step now?

-zm
<<snip>>
 
Z

ZorpiedoMan

A perfect, and complete answer. thanks.

-zm


Jay B. Harlow said:
ZorpiedoMan,
No.

Remember as you stated "Dim as New creates a new object on the heap".


You are initializing the myObj field to the value of the ClassRef field
(parameter).

If you have:
The myObj field is implicitly initialized with the value of Nothing, no
objects are created, this can be rewritten as:


Which explicitly initializes myObj to Nothing.

To create an object you need to use the New keyword, such as:


Or alternatively (more verbosely)


If you want to see what is happening, add a constructor (Sub New) to your
myClass class and single step the code. As the constructor will be called
whenever a new object is created.

Hope this helps
Jay


cleand
<<snip>>
 
C

Cor

Hi Jay B.

A question in the controls newsgroup and this question did bring me to this.
\\\
Private Sub Load_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim nada As New doCtr(Me)
End Sub
End Class
Public Class doCtr
Public Sub New(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
ctr.Text = "CTR"
Dim newdoCtr As _
New doCtr(ctr)
Next
End Sub
End Class
///

This makes iterating to all controls on a form very simple

Or did you see this before?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top