Re: How do I Create 'ragged' arrays in Excel VBA?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Interesting example. I think that what happens is the following: VBA's
garbage collection is based on reference counting. When you run the
statement A(i) = B you are establishing a reference to the current
array B - a reference which won't be removed until *A(i)* is
reassigned (or destroyed). When ReDim B(1 to i) is run a brand new
array is allocated for B. This usually results in the old array being
garbage collected - but here you still have a reference keeping it
alive. Your code seems to work but it is not clear to me that you are
guaranteed that a ReDim B always allocates a separate memory block for
the new array and never overwrites the old B (unless it is ripe for
garbage collection) . In other words - I'm not sure if you have found
a subtle use of a documented behavior or a convienent use of an
undocumented. I suspect the former, but am not 100% sure. Maybe
someone with more knowledge of VBA's memory management will chip in.

-John Coleman


On Mar 11, 1:56 am, David Empey <dem...@xxxxxxxxxx> wrote:
The following code seems to work, but is it safe?

Sub RaggedArray

Dim A() as Variant, B() as Long, i as Long, j as Long

ReDim A(1 to 10)
For i = 1 to 10
ReDim B(1 to I)
A(i) = B
For j = 1 to i
A(i)(j) = 10 * i + j
Next j
Next i

End Sub

This seems to create an array A whose elements are arrays
of varying lengths, which is what I want, but can I be
sure the elements of A won't be overwritten by some other
piece of code that needs to use memory? Does Visual
Basic know the elements of A exist?

Am I even asking a sensible question?

--
Dave Empey

Remember, if you're doing any major experiments in stellar
dynamics, always mount a scratch star first! --Richard Todd


.



Relevant Pages

  • Re: Java Generic programming using subclassing
    ... Java nowhere guarantees that the elements of an array ... Now chances are that any Java ... > reference will be visible through the other. ... > eligible for garbage collection, and will in fact be collected before ...
    (comp.lang.java.programmer)
  • Re: How to destroy arrays
    ... I know when it's eligible for garbage collection. ... When a is an array reference, ... I nowhere readed that implyment of Scott it was only, ...
    (microsoft.public.dotnet.general)
  • Re: Performance issue with filestream.write on huge file
    ... converted to an XML element and outputed to a file. ... by the type array written which are not released by the GC. ... The caller, of course, still may have a reference and if so the bytewon't be eligible for garbage collection no matter what you do in that Writemethod. ...
    (microsoft.public.dotnet.framework)
  • Re: Read very large file in bytearray and upload to MSSQL
    ... and this method just reuses it. ... you did set the reference to the array to Nothing /before/ creating the new ... reference to the old array has been cleared. ... I don't know if it helps and if the next Redim will wait ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to destroy arrays
    ... There's an equivalent to what Erase *actually* does, ... different - but for instance variables, I usually find that the array ... garbage collection - if there's another reference to the same array, ... different array) or become eligible for garbage collection itself. ...
    (microsoft.public.dotnet.general)