Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-03

How to work with reference-counted strings in dynamically created objects

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Howard Page-Clark

The following program, compiles and runs as expected using a shortstring declaration in the TExample record.
Could someone explain how to adapt it to cope with a TExample record containing ansistrings?

Howard

== code ==

program Project1;
{$mode objfpc}{$H+}
{$ModeSwitch advancedrecords}
uses classes, sysutils;
type
   { TExample }
   TExample = record
     text: string; // program performs flawlessly with shortstring;
     procedure Init(const aText: string);
   end;
   PExample = ^TExample;
   { TExampleList }
   TExampleList = class(TObject)
   private
     FList: TFPList;
     procedure Initialize(aCount: integer);
   public
     constructor Create;
     destructor Destroy; override;
     procedure ListItems;
   end;

{ TExampleList }

procedure TExampleList.Initialize(aCount: integer);
var
   i: integer;
   pex: PExample;
begin
   for i:=1 to aCount do begin
     GetMem(pex, SizeOf(TExample));
     pex^.Init(Format('Example#%d, Value=%d',[i, Random(100)]));
     FList.Add(pex);
   end;
end;
constructor TExampleList.Create;
begin
   FList:=TFPList.Create;
   Randomize;
   Initialize(4);
end;
destructor TExampleList.Destroy;
var
   i: integer;
begin
   for i:=0 to FList.Count-1 do
     Freemem(FList[i], SizeOf(TExample));
   FList.Free;
   inherited Destroy;
end;
procedure TExampleList.ListItems;
var
   i: integer;
begin
   for i:=0 to FList.Count-1 do
     WriteLn(PExample(FList[i])^.text);
   ReadLn;
end;

{ TExample }

procedure TExample.Init(const aText: string);
begin
   text:=aText;
end;
var
   exampleList: TExampleList;
begin
   exampleList:=TExampleList.Create;
   exampleList.ListItems;
   exampleList.Free;
end.

---
This email has been checked for viruses by Avast antivirus software. http://www.avast.com

Sven Barth
to Howard Page-Clark
"Howard Page-Clark" wrote
The following program, compiles and runs as expected using a shortstring

declaration in the TExample record.

Could someone explain how to adapt it to cope with a TExample record

containing ansistrings?

[snip]

Show quoted text 7 lines
procedure TExampleList.Initialize(aCount: integer); var i: integer; pex: PExample; begin for i:=1 to aCount do begin GetMem(pex, SizeOf(TExample));
// use New(pex) instead of GetMem(...)
Show quoted text 5 lines
pex^.Init(Format('Example#%d, Value=%d',[i, Random(100)])); FList.Add(pex); end; end;

[snip]

Show quoted text 7 lines
destructor TExampleList.Destroy; var i: integer; begin for i:=0 to FList.Count-1 do Freemem(FList[i], SizeOf(TExample));
// Use Dispose(PExample(FList[i])) instead of FreeMem(...)
FList.Free; inherited Destroy; end;

It's best to always use New/Dispose if you work with records. Use GetMem/FreeMem only if you work with unstructured memory areas or where you don't really know the real size beforehand.

Regards,
Sven

Howard Page-Clark
to Sven Barth
Sven Barth wrote 44 lines
Am 28.03.2015 06:51 schrieb "Howard Page-Clark" < >: > > The following program, compiles and runs as expected using a shortstring declaration in the TExample record. > Could someone explain how to adapt it to cope with a TExample record containing ansistrings? [snip] > procedure TExampleList.Initialize(aCount: integer); > var > i: integer; > pex: PExample; > begin > for i:=1 to aCount do begin > GetMem(pex, SizeOf(TExample)); // use New(pex) instead of GetMem(...) > pex^.Init(Format('Example#%d, Value=%d',[i, Random(100)])); > FList.Add(pex); > end; > end; > [snip] > > destructor TExampleList.Destroy; > var > i: integer; > begin > for i:=0 to FList.Count-1 do > Freemem(FList[i], SizeOf(TExample)); // Use Dispose(PExample(FList[i])) instead of FreeMem(...) > FList.Free; > inherited Destroy; > end; > It's best to always use New/Dispose if you work with records. Use GetMem/FreeMem only if you work with unstructured memory areas or where you don't really know the real size beforehand. Regards, Sven

Thank you

Howard

---
This email has been checked for viruses by Avast antivirus software. http://www.avast.com