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 9316243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:34:06+00:00 2026-06-19T02:34:06+00:00

I often read that the compiler does some kind of magic when casting to

  • 0

I often read that the compiler does some kind of magic when casting to PChar.

The main purpose of PChar is to call foreign code in libraries that expect a zero terminated array of characters (C style “string”).

PChar is a type that is an alias to PWideChar on Unicode Delphi and an alias to PAnsiChar on non-unicode Delphi.

Delphi strings (string = AnsiString / UnicodeString, I’m not talking about WideString here, let alone ShortString …) on the other hand have a hidden length instead of a termination char. They are also reference counted and use copy on write.

Are Delphi strings automatically allocated and kept to be one char longer with an implicit #0 char in order to make casting to a PChar (PAnsiChar / PWideChar) easier or does the compiler check and adjust the string when it encounters a conversion to PChar?

  • 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-06-19T02:34:08+00:00Added an answer on June 19, 2026 at 2:34 am

    The process is as follows:

    • If a string s has length greater than zero, then PChar(s) returns a pointer to the first element of the string content. Because the string is managed to have a hidden null-terminator nothing more needs to be done.
    • If a string s has length zero, then PChar(s) returns a pointer to a block of memory containing a null-terminator.

    As an implementation detail, the null-terminator that is returned from PChar(nil) is a global constant allocated in the read-only section of the compiled module.

    Are Delphi strings automatically allocated and kept to be one char longer with an implicit #0 char in order to make casting to a PChar (PAnsiChar / PWideChar) easier?

    Yes.

    The magic, if you can call it that, is that:

    1. Using PChar() on empty strings returns a pointer to a null-terminator.
    2. Delphi maintains the hidden null-terminator at the end of the string.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often read that one purpose of a VIEW is security: to allow some
I've often read that it's bad from a perf perspective that branching, kind of
I have often read that some programming languages are clear than others and I
I've often read that in the Sun JVM short-lived objects (relatively new objects) can
Code is read more often then updated. Writing more readable code is better than
when i read through source files of opensource projects i often come across some
I read that a m:m relationship often means there is a third class that
Often I read of some software cutting out some C++ features in order to
I often read that linked list data structure and its variant skiplists are cache
I've read that Strategy objects often make good flyweights (from Design Patterns Elements of

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.