Pchar, String, char array in Delphi

Source: Internet
Author: User

Reference: http://blog.csdn.net/procedure1984/article/details/5419616

The difference between one or three people

Both string and char arrays are a piece of memory that holds contiguous characters.

string memory for specific characters is transparent to the user, there is Delphi management of its allocation, replication and release, the user can not intervene (in fact, but through the illegal way)

A char array is similar to an array of characters in C + +

Pchar is a pointer, it is only 32 bits in size. The definition is automatically filled in by Delphi 0. To use Pchar as a string, you must allocate the memory yourself and release it after you have finished using it. the Pchar string represents the end of the string by #0. The operation of the related Pchar string provided by Delphi is to judge #0 to determine the end of the string .

Because Pchar is a pointer, it can point to anywhere (that is, it does not necessarily have to point to a string).

Assign a string to Pchar, just assign the address of the memory that holds the string in string to the Pchar variable. Of course, you can also assign the address of the first element of the char array to Pchar

General Memory Size relationship: Char array <pchar (refers to a string that has been assigned) <string (except for some other information that includes strings in addition to strings)

If it is an empty string, then: PCHAR<STRING<ARRAY[0..N] of Char

From a speed point of no doubt the string is the slowest, for example: when passed as a parameter (not a var call) to the procedure, string assigns the entire string and passes the copy past, Pchar passes the copy of the pointer itself past (32 bits),and the char array is the same as Pchar. The address copy of the first element is passed .

However, in terms of flexibility, the string is the highest, and Delphi supports the most functions. you can also use string as buffer (because it can contain character 0), which is a very important technique .

1. In later versions of Delphi2.0, there are two types of string,

A is compatible with Pascal's traditional string, called shortstring, 
its storage structure is as follows:  
     +- --------------------+    
     | 1Byte |    string Content | 
     0          1 ...  
where the first byte is the length of a string. &NBSP
So the string length that shortstring can include cannot be greater than 255.  
Another is called a long string ansistring,    it is a pointer to a string, but the specific storage is somewhat special.  
Its storage structure is as follows:  
    | 4 B | 4 B |     string content | 
   -8      -4     0    ...  
where ansistring points to the first character of a string,  
in the first character of the inverse direction 1th through 4th of 4 bytes representing the length of the string, The number of times the 5th to 8th 4-byte table string is referenced.

Ansistring is unique to Delphi, and its storage is dynamically allocated at run time and has auto-recycle function, because this feature ansistring sometimes referred to as the lifetime self-management type .

When two or more ansistring types share a reference to the same physical address, Delphi uses the copy–on-write Technique, a string to wait until the modification is complete, To release a reference and assign a physical string. The following examples help to understand these concepts:

var  S1, S2:string;begin  s1:= ' Example for copy-on-write ... ';//assign value to s 1, the reference count of S 1 is 1  s2:=s1;                            Now s 2 and S 1 point to the same string, the reference count of S 1 is 2  s2:=s2+ ' Changed ';               s 2 is now changed, so it is copied to its own physical space, and the reference count of S 1 is reduced by 1. End

2.pchar is a pointer to a purely string (#0字符结尾), the same as char * in C.

The 3.char array is also a pointer to a string, which differs from Pchar in that:
1. A char array (which means non-dynamic arrays) once defined, its length is fixed;

2. The address of a char array is constant, cannot be assigned another value, and cannot be like Pchar.
such as: Spchar:pchar; Sarray1,sarray2:array[0..80]of Char;
Spchar:=sarray2; Spchar;=sarray1;
But not sarray2:=sarray1;

4. Introduction to the difference of the three

The fastest, of course, is the Pchar and char arrays with pure pointer operation the fastest, so-called memory is the least, more efficient,

I don't know, man, what do you want to do with the application, generally on String,pchar or char arrays, regardless of these.

For programming, if used in Delphi or C++builder, as far as possible to use Ansistring,borland company for it has been very perfect internal processing, the use of very convenient.

If you are involved in Windows APIs or mixed programming, the interface section generally uses Pchar.

Char arrays use less, because most places where you can use a char array, you can use Pchar to allocate memory instead

It is now more popular to define a ansistring and then use SetLength to set its length .

5. The difference and contact between a string character array and a pointer to a string pchar

The basic concepts of these 3 are the same, but there are some very subtle differences that can be error-prone when programming, and require a high degree of attention.

  use a pointer to a string, and if it does not end with 0, an error occurs at run time. To avoid this error, you need to manually add 0, char (0) at the end of the string, or use the Strpcopy function to automatically add 0 to the end of the string.

Example 1: A pointer to a string, and if it does not end with 0, an error occurs at run time:

{s[0]=3 s[1]= ' n ' s[2]= ' e ' s[3]= ' W '}var    s:string;    P:pchar;begin    s:= ' new ';    label1.caption:= s;    {' New '}    label2.caption:= IntToStr (Integer (s[0]));    {3 is the length of the string}    By accessing the string variable's s[0] to get the length of the string, corresponding strings stored in s[1] and s[1], and string string is not the end of the 0    p:= @s[1];    {Not ending with 0, MO pchar-type pointer}    Label3.caption:=strpas (p);    {error}end at runtime;

Example 2: Manually adding 0, or char (0) at the end of a string, but with a pointer to a string

{s[0]=4 s[1]= ' n ' s[2]= ' e ' s[3]= ' W ' s[4]=0}var    s:string;    P:pchar;begin    p:[email protected][1];    P points to the address of s[1], because string type strings are stored in s[1] and beyond, while s[0] partially stores the length of the string        s:= ' new ' +char (0);    {ending with 0, you can use the Pchar-type pointer    label1.caption:= Strpas (p); end;


Mutual conversion between the two and three

The following three variables are available

var    s:string;    P:pchar;    A:ARRAY[1..20] of Char;

So the conversion between the three is as follows

1. String to Pchar

p:= PChar (s);//Pay attention to the difference between PChar and string////p:= PChar (S+char (0) strictly); Of course, the above method is also possible, because it will be processed and optimized at compile time.

2.Pchar to String

S:= p;

3.PChar to character array

Strcopy (@a, p);

4. Character array to Pchar

PChar (@a);

5. Conversion between a string and a character array

The conversion between a string and a character array can only be brokered by Pchar. Cases

Procedure Tform1.btnclick (sender:tobject); var    str:array[0..10] of Char;begin    strcopy (@str, PChar (mmo1. Text));    Mmo2. text:= PChar (@str); end;


Pchar, String, char array in Delphi

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: info-contact@alibabacloud.com 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.