473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

A stack/heap question

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?

Nov 20 '05 #1
11 1367
In article <ef*************@TK2MSFTNGP11.phx.gbl>, ZorpiedoMan wrote:
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.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2
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


"ZorpiedoMan" <xyz@abc> wrote in message
news:ef*************@TK2MSFTNGP11.phx.gbl...
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?

Nov 20 '05 #3
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

"ZorpiedoMan" <xyz@abc> wrote in message
news:ef*************@TK2MSFTNGP11.phx.gbl... 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?

Nov 20 '05 #4
"ZorpiedoMan" <xyz@abc> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
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
Nov 20 '05 #6
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

"Cor" <no*@non.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #7
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ur*************@TK2MSFTNGP10.phx.gbl...
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

"Cor" <no*@non.com> wrote in message
news:ei**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #8
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" <xyz@abc> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... 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>>
Nov 20 '05 #9
A perfect, and complete answer. thanks.

-zm
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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" <xyz@abc> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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>>

Nov 20 '05 #10
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
Nov 20 '05 #11
Cor,
Yes I've seen variations of that before.

Thanks
Jay

"Cor" <no*@non.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
0
by: CoderGuy | last post by:
Hello I am reading up a bit on how memmory is used in the .NET Framework and have a few question about the stack-based approach * I understand that the stack is used to provide a layer of...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
11
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting...
3
by: balu | last post by:
Hi Gurus I need a help. Im developing a C++ libaray. I don't whether user creates object in stack/heap. Is there a way to find object location? Balu.N
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.