Board index » delphi » Do local dynamic arrays go away?

Do local dynamic arrays go away?

Simple question,looked in help but didn't see the answer,

consider

  procedure test;
var
  a : array of real;
begin
  setsize(a,20);

  // do stuff to a here
End;

Do I have to release a with a := Nil or will the exit code do it for me?

Thanks

-John_Mer...@Brown.EDU

 

Re:Do local dynamic arrays go away?


Quote
John_Mertus wrote:
> Simple question,looked in help but didn't see the answer,

> consider

>   procedure test;
> var
>   a : array of real;
> begin
>   setsize(a,20);

>   // do stuff to a here
> End;

> Do I have to release a with a := Nil or will the exit code do it for me?

it is done automatically.  Dynamic arrays are reference counted like strings
so once all the references go out of scope the memory for the array is
de-allocated.

--
Bob Lee
High Performance Delphi - http://www.econos.com/optimize/

Re:Do local dynamic arrays go away?


However if you were allocating memory for an array of objects or
pointers instead of reals, you would need to deallocate before leaving
the scope of the procedure.

Mitch Wolberg,
RockWare, Inc.

Quote
Robert Lee wrote:

> John_Mertus wrote:

> > Simple question,looked in help but didn't see the answer,

> > consider

> >   procedure test;
> > var
> >   a : array of real;
> > begin
> >   setsize(a,20);

> >   // do stuff to a here
> > End;

> > Do I have to release a with a := Nil or will the exit code do it for
> me?

> it is done automatically.  Dynamic arrays are reference counted like
> strings
> so once all the references go out of scope the memory for the array is
> de-allocated.

> --
> Bob Lee
> High Performance Delphi - http://www.econos.com/optimize/

Re:Do local dynamic arrays go away?


Quote
Mitch Wolberg wrote:

> However if you were allocating memory for an array of objects or
> pointers instead of reals, you would need to deallocate before leaving
> the scope of the procedure.

   Just as you would if it were a single object, rather than an array of
them. Just helping the original poster understand your point a little
more. :-)

--
Marko Peric       lonew...@tig.com.au
http://homepages.tig.com.au/~lonewolf

Re:Do local dynamic arrays go away?


Im Artikel <UdqV2.838$zH2....@news.rdc1.ct.home.com>, John_Mer...@brown.edu
(John_Mertus) schreibt:

Quote
>Do I have to release a with a := Nil or will the exit code do it for me?

It's safe to set a := Nil, but not required.

Dynamic arrays reside in the global heap, and are reference-counted. This
handling is reflected in the different convention to release such objects, by
setting the reference variable to Nil, in contrast to calling a destructor or
Free for other objects, that do not use reference counting.

The "var a" resides on the stack, and in fact is only a reference to the heap
object. All such references are released properly when a subroutine terminates,
everything else were a bug in the implementation. "Release" here means, that
the reference count of the global object is decremented, but the object
consequently is destroyed only, if the reference count goes down to zero, i.e.
when the last reference to the object is invalidated.

The String data type uses exactly the same mechanism, so all local dynamic
strings of a subroutine are released on exit from the subroutine. The Result
pseudo-variable is one such reference, that keeps the resulting string alive
even after exit from the function.

BTW, local variables of the old static string type, limited to 255 characters,
just like local static arrays continue to reside on the stack. An attempt to
return a reference to such a string, using e.g. a PChar, will most likely
produce errors. The memory location, pointed to by that PChar, can be
overwritten with the arguments or local variables of every subsequent
subroutine call. The result of a function of type String is a dynamic string
object, therefore an attempt to return a local static string from the function
always results in a copy of the static string into a new dynamic string object
in the global heap. A similar attempt with fixed arrays, if allowed at all,
will require an equivalent copy of the local static array into an equivalent
dynamic array, for the function result.

DoDi

Re:Do local dynamic arrays go away?


John_Mertus <John_Mer...@brown.edu> schrieb in im Newsbeitrag:
UdqV2.838$zH2....@news.rdc1.ct.home.com...

Quote
> Simple question,looked in help but didn't see the answer,

> consider

>   procedure test;
> var
>   a : array of real;
> begin
>   setsize(a,20);

>   // do stuff to a here
> End;

> Do I have to release a with a := Nil or will the exit code do it for me?

> Thanks

> -John_Mer...@Brown.EDU

No - I think not. It's still a local variable - don't matter whether it is
dynamic. All local var's are released at the end of a procedure - except
pointers (eg. classes).

Cheers,
Marc

Other Threads