Delphi character and string [3]

Source: Internet
Author: User
Tags integer

//String 的指针地址及实际的内存地址
str: string;
pstr: PString;
pc: PChar;
{在没有给 str 赋值以前, 既然声明了, 就有了指针地址 (@str):}
ShowMessage(IntToStr(Integer(@str))); {1244652; 这是在栈中的 str 的指针地址}
ShowMessage(IntToStr(Integer(str))); {0; 0 就是 null}
str := 'Delphi';
ShowMessage(IntToStr(Integer (@str))); {1244652; 这是在栈中的 str 的指针地址}
ShowMessage(IntToStr(Integer(str)));  {4580800; 这是在堆中的 str 的实际地址}
{通过指针地址获取字符串, 其中的 pstr 是前面定义 的字符串指针}
pstr := @str;
ShowMessage(pstr^); {Delphi}
{通过实际地址获取字 符串, 其中的 pc 是前面定义的字符指针}
pc := PChar(Integer(str));
ShowMessage(pc);   {Delphi}
A string (ansistring or string, such as "Form1") is stored in memory in this way:

F O R M 1

The yellow area is where the string is actually stored, and the memory address of the string that precedes it is the location of the "F" in this example;

The blue four bytes store an Integer value representing the length of the string;

The last red byte stores a null character (#0) that represents the end of the string and is compatible with the null-terminated string of Windows;

A green Four byte is also an Integer value that indicates the number of times the string was referenced (that is, a pointer to a few strings pointing to it).

Let's take a look at var
str,s1,s2: string;
pint: PInteger;
str := Self.Text; {把窗体标题给它吧; 现在 str 指向了窗体标题所在的内存位置}
s1 := str;     {给 s1 赋值}
s2 := str;    {给 s2 赋值; 现在窗体标题已经有了 str、s1、s2 三个引 用}
{str、s1、s2 的指针肯定不一样; 但现在指向内存的同一个位置, 测试:}
ShowMessage (IntToStr(Integer(str))); {15190384}
ShowMessage(IntToStr(Integer(s1))); {15190384}
ShowMessage(IntToStr(Integer(s2))); {15190384}
{向左偏移 4 个字节就是字符串长度的位 置, 读出它来(肯定是5):}
pint := PInteger(Integer(str) - 4);
ShowMessage(IntToStr (pint^));   {5}
{向左偏移 8 个字节就是字符串的引用计数, 读出它来(肯定是3):}
pint := PInteger(Integer(str) - 8);
ShowMessage(IntToStr(pint^));   {3}
An example: when the reference count for a segment of string memory is 0 o'clock, Delphi automatically releases it; This is why the string does not need to be manually freed.

I found in the test that the reference count for all constant and non global variables has been "-1".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.