Re: Arrays
- From: "Bill McCarthy" <Bill@xxxxxxxxxxxxx>
- Date: Wed, 24 Sep 2008 01:50:28 +1000
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
>
.
- Follow-Ups:
- Re: Arrays
- From: Lorin
- Re: Arrays
- References:
- Arrays
- From: Bob
- Re: Arrays
- From: Bill McCarthy
- Re: Arrays
- From: Lorin
- Arrays
- Prev by Date: Re: Arrays
- Next by Date: Re: Compressing a xml file using compression algorithms
- Previous by thread: Re: Arrays
- Next by thread: Re: Arrays
- Index(es):
Relevant Pages
|