?
Solved

String vs. PChar

Posted on 2004-04-05
4
Medium Priority
?
436 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:__alex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10757496
Yes, you are allowed to free this buffer, as the memory for the list is allocated internally to the list

Regards,
Russell
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 10757511
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
 
LVL 2

Author Comment

by:__alex
ID: 10757619
Thanks Russel!
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10757660
You are very welcome, and thank you

Russell

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question