View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jim Thomlinson Jim Thomlinson is offline
external usenet poster
 
Posts: 5,939
Default VBA: What does Redim Preserve do in term of memory

ReDim Preserve copies the existing array to a new location with a larger
memory allocation. So yes it has a whole pile of overhead. Redim just creates
the new larger memory allocation without copying the data from the existing
array so there is very little overhead.

If performance is a big concern then there are a couple of strategies to
deal with this.

One is to do some work to determine how many elements you will need ahead of
actually decalraing the size of the array. Then you only need to declare the
array once.
Another is to redim a much larger memory space than is necessary by adding
say 100 elements instead of just 1. You can then keep adding to the array
until all 100 of the extra spots have been used up at which point you would
need to onece again redim preserve 100 more spots. At the end when you know
how many spots you have actually used you can clean up the final array to the
correct size.
--
HTH...

Jim Thomlinson


"Charles" wrote:

Hello

a somewhat basic VBA question, but I can't find the answer on google.

What does Redim Preserve do in practice in term of memory. Does it
recopy every single element of an array in a new one with a different
dimension, as this article seems to say:

http://msdn2.microsoft.com/en-us/lib...s2(VS.80).aspx

or is it a bit more clever, and finds a way to add/remove dimensions
without recopying the data.

The question is only relevant in the context of large amounts of data.
What I am trying to figure out is if I have large amounts of data, is
it worth bother building a system of dynamic arrays with objects, or
is the Redim statement good enough.

Thanks in advance for your answer!
Charles