String vs. PChar

procedure Foo(buffer: PChar);
begin
  ...
  // list: TStringList;
  list.Text := buffer;
  ...
end;

Am I allowed to free "buffer" after calling Foo, that is do I get a copy of the memory "buffer" points to?
LVL 2
__alexAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Russell LibbyConnect With a Mentor Software Engineer, Advisory Commented:
See the following (SetString in paticular):

procedure TStrings.SetTextStr(const Value: string);
var
  P, Start: PChar;
  S: string;
begin
  BeginUpdate;
  try
    Clear;
    P := Pointer(Value);
    if P <> nil then
      while P^ <> #0 do
      begin
        Start := P;
        while not (P^ in [#0, #10, #13]) do Inc(P);
        SetString(S, Start, P - Start);
        Add(S);
        if P^ = #13 then Inc(P);
        if P^ = #10 then Inc(P);
      end;
  finally
    EndUpdate;
  end;
end;

Russell
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Yes, you are allowed to free this buffer, as the memory for the list is allocated internally to the list

Regards,
Russell
0
 
__alexAuthor Commented:
Thanks Russel!
0
 
Russell LibbySoftware Engineer, Advisory Commented:
You are very welcome, and thank you

Russell

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.