A brief analysis of the string in Delphi (Ladybug, there is an example of memory error)

Source: Internet
Author: User
Tags ord

A brief analysis of the strings in Delphi

Just shallow analysis, so that everyone can quickly understand the string.

All of the code is tested and passed under Delphi7.

The string types in Delphi 4,5,6,7 include the following:

    • Short string
    • Long string
    • Wide string (Wide string)
    • 0 Ending string (null-terminated string), Pchar, and character array

1. Short string

Fixed length, the maximum number of characters is 255, and the short string becomes the length byte (length-byte) string, because the No. 0 element of a short string contains the length of the string (the number of characters in the string). So the default maximum length for shortstring is 256 bytes (255 characters + 1 length bytes =256), and there are two ways to declare a short string, as follows:

var  s:shortstring;   {255 character length, 256 bytes}  S1:STRING[255];  {S1 and S have the same character type}   Len:integer;begin  S: = ' Hello ';  Len: = Ord (S[0]); {Len now contains the length of S as 5,ord function can convert a character type to an integer type}  Len: = SizeOf (S); {Len now contains the size of the shortstring type, which is 256 bytes}  End

In the example above, the string length of s can be obtained by s[0], and the length of a short string can also be determined by using length function.

You can use the subscript of an array to access a character in a particular position in the shortstring, using the following examples and notes:

var  s:string[8];  I:integer;begin  S: = ' a_pretty_darn_long_string ';  {Because S is only 8 characters in size,  so s actually stores the contents "A_pretty"}  I: = ten;  S[i]: = ' s ';  {Because S is only 8 characters in size,  trying to rewrite the 10th element will make the memory clutter}end;

2, long string

A long string (ansistring) is a dynamically allocated string whose size is limited only by the available memory. To declare a long string, simply use the keyword string without the size parameter.

The characters contained in Ansistring in Delphi 7 are stored in a single byte.

var  s:string;

Because it is dynamically allocated, you can modify the string at once, without worrying about other effects or cross-border issues. The string type has no 0 elements, and attempting to access a string of 0 elements produces a compilation error.

Long string lengths can also be obtained through the length function, or long strings can be set by the SetLength procedure. It is allocated in memory as follows:

3, wide string (Wide string)

The size of a wide string, like a long string, is limited only by valid memory and is dynamically allocated.

In Delphi 7 Widestring is implemented as 2 bytes to store a character, it is very convenient to use widestring to handle multibyte characters. Such as:

var  s:string;  {The default string in Delphi 7 is equivalent to ansistring}  Ws:widestring;begin  S: = ' Hello World ';  WS: = S;  ShowMessage (S[1]);  {There is no display at this time, because S[1] takes out half  of the ' world ' ShowMessage (Ws[1]); {Show ' World '}end;

4, 0 ending string (null-terminated string), Pchar, and character array

In C and C + +, there is no real string data type, all by a null-terminated (0) character array, the character array has no length byte, so only the trailing null flag is used as the character ending flag of the string. And because Windows is written in C, many Windows functions use character arrays as parameters, but the Pascal string type is not a character array, because in order for Pascal strings to be compatible with Windows, a string array is required. The Pchar type is exactly what this requires, and Pchar is available wherever a character array is required.

Although both ansistring and widestring have been implemented with NULL

The corresponding Pansichar and Pwidechar, respectively, correspond to Ansichar characters and widechar characters.

For example: The Windows MessageBox function, which declares the following:

function MessageBox (hwnd:hwnd; lptext, Lpcaption:pchar; utype:uint): Integer; stdcall;

The second and third parameters require a pointer to a character array, in order to call this function, there are three ways to implement the

1. PChar () type conversion

var  text:string;  Caption:string;begin  Text: = ' This is a test. ';  Caption: = ' Test Message ';  MessageBox (0, PChar (Text), PChar (Caption), 0);  {This pchar is used to convert string type to null-terminated}end;

which

2. Pchar variable

Let's start with an implementation and see what the Pchar type is.

Run the following program:

var  text:pchar;    {declared as Pchar type}  str:string;    {declared as String type}begin  Text: = ' This is a test. ';             {All are given the same string}  STR: = ' This is a test. ';  ShowMessage (IntToStr (SizeOf (Text)));   {4 bytes, essentially a pointer}  ShowMessage (IntToStr (SizeOf (STR)));    {also 4 bytes, also pointer}end;

Through the program above, we know that text is just a pointer.

var  text:pchar;begin  Text: = ' This is a test. ';  MessageBox (0, Text, ' Test Message ', 0);  {Here the text is directly declared in order to Pchar type, string constants can be directly used}end;

The pointer text points to a region of memory that contains a null end of the ' This is a test ' string. It is equivalent to the following code:

Const  tempstring:array[0..15] of Char = ' This is a test. ' #0; var  text:pchar;begin  Text: = @TempString [0];  {text points to the address of the No. 0 element of the null-terminated tempstring character array, which is the  first address of the entire character array}  MessageBox (0, Text, ' Test Message ', 0); end;

3. Char type character array

You can also use a char array instead of Pchar, the code is as follows:

var  text1:array[0..14] of Char;  {size is 15 characters}  TEXT2:ARRAY[0..20] of Char;  {The size is 21 characters}begin  Text1: = ' This is a test. ';   {TEXT1 and Text2 have a character length of 15 characters}  TEXT2: = ' This is a test. ';  MessageBox (0, Text1, ' Test Message 1 ', 0);  {because the Text1 character length exceeds its declared size, because memory access is confusing, displaying a scramble}  MessageBox (0, Text2, ' Test Message 2 ', 0);  {Text2 character length is smaller than the declared size because normal access shows correct}end;

The results appear as follows:

The string on the first shallow talk about this, and then in-depth understanding.

Http://www.cnblogs.com/pchmonster/archive/2011/12/14/2287686.html

A brief analysis of the string in Delphi (Ladybug, there is an example of memory error)

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.