Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:44:53+00:00 2026-05-29T09:44:53+00:00

var S: string; begin SetLength(S, MAX_SIZE);// when casting to a PChar, be sure the

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:44:54+00:00Added an answer on May 29, 2026 at 9:44 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this code works fine: procedure TForm2.Timer1Timer(Sender: TObject); var Text: string; begin SetLength (Text,555); GetWindowText
Sure, even I can do this... var testarray : array of string; setlength(testarray, 5);
Code Illustration procedure TForm1.FormCreate(Sender: TObject); var Str: string; PStr: PChar; begin Str := 'This
function trim(str,options){ var string = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length, j
Let's say I have this string var: string strData = 1|2|3|4||a|b|c|d Then, I make
I have an array: var locations = ['Afghanistan','Albania','Algeria','New York']; and a string: var string
var is:ImageSnapshot = myImagesnapshot; var str:String = ImageSnapshot.encodeImageAsBase64(is); As of now, I am sending
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do
HI I have a URL var str:String = conn=rtmp://server.com/service/&fileId=myfile.flv or var str:String = fileId=myfile.flv&conn=rtmp://server.com/service/
Say for example I have the following string: var testString = Hello, world; And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.