Re: Dyn Arrays



Alex Moore wrote:

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?

It won't leak memory, but redimensioning it (using SetLength) on each
iteration could very well fragment it, especially for a large N. It
could also become very slow, for a large N, since each time the array
must be copied over to a newly allocated array, by the RTL.

Have a look at my view on such practices:

http://blogs.teamb.com/rudyvelthuis/archive/2004/06/26/644.aspx

--
Rudy Velthuis http://rvelthuis.de

"There is no reason anyone would want a computer in their home."
-- Ken Olson, president, chairman and founder of Digital
Equipment Corp., 1977
.