Re: Dyn Arrays
- From: "Tom de Neef" <tdeneef@xxxxxxxx>
- Date: Sat, 23 Jun 2007 16:55:23 +0200
"Alex Moore" <alex.moore@xxxxxxxxxxx> schreef in bericht
news:MY9fi.17626$wH4.16440@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Can I use SetLength to redimension a dynamic array.
For Instance can I do somthing like:
for I:= 1 to N do
begin
SetLength(MyArray, I);
Do Something with MyArray[I - 1];
end;
I am told that this leaks memory. Is that correct?
Yes, you can. And it will not leak memory.
If you resize to a smaller size, information at the end is lost. If you
resize to a larger size, the new elements will be undefined (in most cases,
see Help).
When increasing, new memory will be allocated, the old data will be copied
and the old memory will be released.
When decreasing, the same will happen when the array has a reference count
bigger than 1. If the array is not shared the decrease will act on the
memory already allocated to the array, which is faster.
About your example:
- the range is 0..length(array)-1 so MyArray[i-1] will be OK
- "Do something" must start with an assignment to the array element, since
it will be undefined
- when N is large, this will be a time consuming algorithm
Tom
.
- References:
- Dyn Arrays
- From: Alex Moore
- Dyn Arrays
- Prev by Date: Dyn Arrays
- Next by Date: Re: Dyn Arrays
- Previous by thread: Dyn Arrays
- Next by thread: Re: Dyn Arrays
- Index(es):
Relevant Pages
|