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

Marc Santhoff M.Santhoff at t-online.de
Wed Jul 23 17:15:13 CEST 2008


Am Mittwoch, den 23.07.2008, 08:28 +0100 schrieb Jonas Maebe:
> 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.

Mumble ...

>  And pointer(s):=nil would create a memory leak.

That's the important part, I'm dealing with cleanly freeing strings when
destroying objects.

> > And more: is the memory consumed by the string freed using one of the
> > two methods?
> 
> It is freed if you use s:='' 

This is the way to do it, OK.

> (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).

I will. 

Many thanks,
Marc





More information about the fpc-pascal mailing list