SysUtilsStrMove (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows two functions that do the same thing as StrNew and StrDispose.

Code

function AHeapaString(S: PChar): PChar;
{ Allocate string on heap. }
 var
  L: Cardinal;
  P: PChar;
  StrNew: PChar;
begin
  StrNew := nil;
  if (S <> nil) and (S[0] <> #0) then
  begin
    L := StrLen(S) + 1;
    GetMem(P, L);
    StrNew := StrMove(P, S, L);
  end;
  Result := StrNew;
end;

procedure DisposeDaString(S: PChar);
{ Dispose string on heap. }
begin
  if S <> nil then FreeMem(S, StrLen(S) + 1);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  S, otherS: PChar;
begin
  S := 'Now you see it';
  otherS := AHeapaString(S);
  Canvas.TextOut(5, 10, otherS);
  Canvas.Refresh;
  Sleep(5000);
  DisposeDaString(otherS);
end;

Uses