Solved

String vs. PChar

Posted on 2004-04-05
4
427 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
  • 3
4 Comments
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
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 125 total points
Comment Utility
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
Comment Utility
Thanks Russel!
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
You are very welcome, and thank you

Russell

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now