Re: Arrays



If you copy over pointer types (strings or any objects), then you get a memory leak. That object has to be set to nothing before being copied over. Also any space no longer used, where the object pointers have been copied should be zero filled. I haven't looked closely at your implementation, but when shifting blocks around inside a multi dimension array, these potentials for leaks are very real. Plus, if you are shrinking the array, the system doesn't actually let go of that extra data



"Lorin" <Lorin@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:6CCA5BA1-AA58-4444-95A2-5DEBA80694F8@xxxxxxxxxxxxxxxx
As long a pointers are properly and timely restored/maintined, no memory
leaks occur.
I have been running the code in both a test harness and in a real
applications with no problems.

"Bill McCarthy" wrote:

Hi Bob,

If you are careful, and the data are simple value types like double or long,
then you can use copy memory to move the blocks around as a mutli
dimensional array is just a continuous block of memory. You could then
adjust the array dimensions via the safe array, but you are probably going
to leak memory doing that.

alternatively you could use jagged arrays, although the support for them in
VB6 is limited a bit, e.g:

Dim columns() As Variant
Dim row() As Long
Dim i As Long, j As Long

ReDim columns(0 To 5)
For i = 0 To 5
ReDim row(0 To 10)
columns(i) = row
Next i

For i = 0 To UBound(columns)
For j = 0 To UBound(columns(i))
columns(i)(j) = i * 10000 + j
Next j
Next i

For i = 0 To UBound(columns)
For j = 0 To UBound(columns(i))
Debug.Print i, j, columns(i)(j)
Next j
Next i

ReDim Preserve columns(0 To 3)
For i = 0 To UBound(columns)
row = columns(i)
ReDim Preserve row(0 To 5)
columns(i) = row
Next i

For i = 0 To UBound(columns)
For j = 0 To UBound(columns(i))
Debug.Print i, j, columns(i)(j)
Next j
Next i



Note that to re-dimension a row you need to copy it you, change it's size
and copy it back in

The fastest alternative is likely to be to create a new array of the correct
size and copy the data over to it.




"Bob" <someonw@xxxxxx> wrote in message
news:%232eP%238OHJHA.3960@xxxxxxxxxxxxxxxxxxxxxxx
> Hi Everyone:
>
> I have a 2D array A(n,n). At times, I may need to decrease the size of
> the array by one, like
>
> Redim B(0 to n-1,0 to n-1)
> for i=1 to n
> for j=1 to n
> B(i-1,j-1)=A(i,j)
> next j
> next i
>
> Is this the easiest and fastest way to achieve this, or is there a > better
> way. Thanks for your help.
>
> Bob
>


.



Relevant Pages

  • Re: Tracking Memory leak
    ... Some compilers leak memory when a function returns either an ... array or a pointer to an array. ... memory, then experimenting with "some_size" might help ... it would help if you mentioned the compiler brand name, ...
    (comp.lang.fortran)
  • Re: Memory Leak Issue
    ... EmptClearto pack the array. ... any memory dumps or crash dumps generated so far on my machine, ... It is also showing some of the memory leak in the MFC dlls. ... Also note that the problem with growing private bytes count can be other than a memory leak. ...
    (microsoft.public.vc.debugger)
  • Re: Arrays
    ... Plus, if you are shrinking the array, the system ... then you can use copy memory to move the blocks around as a mutli ... to leak memory doing that. ... ReDim columns ...
    (microsoft.public.vb.general.discussion)
  • Re: What does Redim Preserve do in term of memory
    ... Kind of a picky point but I do not believe that the original array is ... The memory for the old array is just returned to the operating ... and the ReDim Preserve again when the resized array's ... Dim AllocatedToBound As Long ...
    (microsoft.public.excel.programming)
  • Re: rediming an array smaller
    ... >> So there is no zeroing of memory, also I suspect that memory ... >array fit inside that size, ... >I have to agree with the ReDim Preserve for going smaller. ...
    (microsoft.public.vb.general.discussion)