Where is array data kept?

D

David Harmon

With regard to a declaration like:
Dim myIntArray As Integer = new Integer(6)

Jesse Liberty, in _Programming Visual Basic .NET, 2nd Edition_
(O'Reilly) says:
While VB.NET arrays are reference types, created on the heap, the
elements of an array are allocated based on their type. Thus,
myIntArray is a reference type allocated on the heap, and the integer
elements in myIntArray are value types, allocated on the stack. (While
you can box a value type so that it can be treated like a reference
type, as explained in Chapter 6, it is not necessary or desirable to
box the integers in an array.) By contrast, an array that contains
reference types, such as Employee or Button, will contain nothing but
references to the elements, which are themselves created on the heap.

Question:
Is that true? And if so, what happens when you return and the stack
gets popped, while a reference to the array created on the heap may
still exist somewhere?
 
J

Joseph Bittman MCAD

June 16, 2005

Seems to be a bit more complicated than needed! :)

--
Joseph Bittman
Microsoft Certified Application Developer

Web Site: http://71.35.110.42
Dynamic IP -- Check here for future changes
 
C

Cor Ligthert

David,

I assume that the writer explains this. If it is only written to impress the
reader with the knowledge of the writer, than I know what I have to do with
such a book.

For me this is in the part why .Net has managed code. What you ask is
managed by the framework. For me the only important in this that things on
the stack are immutable, and therefore have to be created every time again
on a new place (on that stack) when the format changes (for a string is that
with every change), while objects are mostly only references of references
and therefore very dynamic.

For using something there is no difference with something on the stack or in
an object. When there is no reference anymore than an object can exist,
however for an object, the managed code takes care to release it on a
proper time and you cannot reach it anymore even before that. (While the
stack is removed (probably not really but only decremented with one), every
time that the procedure where it is created in, goes out of scope).

For me is the only part in this that we have to remember, that every time
that when things have to be new created that than consumes processing time.

I hope this gives an idea.

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