[fpc-pascal] freeing string equal to setting to empty string?

Jonas Maebe jonas.maebe at elis.ugent.be
Wed Jul 23 09:28:15 CEST 2008


On 23 Jul 2008, at 06:32, Marc Santhoff wrote:

> I understand right that (as I have asked before) an empty (ansi)string
> is equal to NIL.

True.

> If this is correct, is setting a string to '' the same as setting the
> string to NIL?

No.

> So i can e.g. use exchagably:
>
> {$H+}
> var
>  s: string;
> begin
>  s := 'something';
>
>  { this ... }
>  s := '';
>  { ... works equally to that: }
>  s := NIL;

No, because "s:=nil" doesn't compile. And pointer(s):=nil would create  
a memory leak.

> And more: is the memory consumed by the string freed using one of the
> two methods?

It is freed if you use s:='' (but keep in mind the earlier discussion  
about reference counted types: the memory may not be freed  
immediately, because there may still be hidden references lingering  
around for a while).


Jonas



More information about the fpc-pascal mailing list