2
var
  S: string;
begin
  SetLength(S, MAX_SIZE);// when casting to a PChar, be sure the string is not empty
  SetLength(S, GetModuleFilename(0, PChar(S), Length(S)));
  // statements
end;

To eliminate the overhead of copying the buffer, you can cast the string to a PChar (if you are certain that the routine does not need the PChar to remain in memory). http://docwiki.embarcadero.com/RADStudio/en/Passing_a_Local_Variable_as_a_PChar

We have a string and it will remain in memory until its reference count decremented to 0, so it will not be removed in the course of scope. So, why do we need PChar to remain in memory? Is there some API function that require the exact same PChar reference that is passed to another API function previously?

Especially i am considering the sample code. So, the question must be "why would a routine need a PChar(type-cast from a string) passed to it remain in memory after it returns?". Async i/o routines, or an async method that access the passed pchar after the caller returns, or modify of a global string by another thread are good reasons.

lockedscope
  • 965
  • 17
  • 45
  • Think about PChar variables as *references* to some entity - in your case long string. When entity being deallocated - all the references to it are becoming invalid. – OnTheFly Feb 08 '12 at 16:17
  • that's ok but it will not become invalid within the course of a routine. it could be possible if it's a global string variable and another thread assigns it some value to cause the string reference invalid. – lockedscope Feb 08 '12 at 16:49
  • Suppose some badly designed routine a) keeps the reference instead of actual data b) starts the thread, which still runs when your routine exits c) tries to access the data. Here we go - A/V. – OnTheFly Feb 08 '12 at 17:57
  • 1
    Or consider `function SomeMessage(SomeCode: Enumerated): PChar`. With such routine you can return constants, but attempts to add some prefix to the string before returning will fail. – OnTheFly Feb 08 '12 at 18:01
  • 2
    @user Keeping the reference isn't necessarily bad design. Methods just must document that. – CodesInChaos Feb 08 '12 at 20:36

1 Answers1

6

Sometimes the API stores the pointer until a later time. For example think of async IO.

Or if you wrote code like the following, it'd fail too:

function Broken():PChar;
var s:string;
begin
  s:=IntToStr(Random(100));//no string constant
  return PChar(s);
end;

One important point is if you cast a string with refcount>1 to PChar, it will create a copy, and then hand you a string with refcount=1. So your PChar becomes invalid once the s you passed in becomes invalid, even if it had a refcount>1.

Another broken example:

var p:PChar;
var s:string;

s := IntToStr(Random(100));//no string constant
p := PChar(s);
s := "ABC";
DoSomething(p);//p is invalid by now
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262