Data Type PChar of Delphi

Source: Internet
Author: User

VaR
S: string;
PC: pchar;
Pb: pbyte;
AC: array [1 .. 100] of char;
AB: array [1 .. 100] of byte;
I: integer;
Begin
S: = 'this is a test ';
PC: = pchar (s); // string-> pchar
Pb: = pbyte (PC); // pchar-> pbyte
For I: = 1 to length (s) do
Begin
AC [I]: = s [I]; // string-> arrary of char
AB [I]: = byte (s [I]); // string-> arrary of byte
End;
S: = pc; // pchar-> string
S: = string (PB); // pbyte-> string
S: = C; // arrary of char-> string;
End;
Move (buffer ^, pchar (@ rbuf) ^, bufferlength); this is correct, but I don't know much about pchar (@ rbuf) ^, can someone tell me in detail?
Move (buffer ^, // -- get the value of the buffer pointer to unreference;
Pchar (@ rbuf) ^, // -- @ rbuf: This is the entry address of your receiving buffer. Equivalent to: @ rbuf [0];
// Pchar (@ rubf) This is to convert your receive buffer pointer to pchar type pointer; that is ^ char
// (Pchar (@ rbuf) ^ in this case, the column is clear. After removing the pointer reference, the value is actually the value in the pointer address.
Bufferlength); move is to copy data between two addresses, move (D, S, Len) is to copy len bytes from s address, then store the data in the LEN byte space starting with D. D and S do not necessarily require pointers, such
Move (buffer, rbuf, bufferlength );
Or
Move (buffer, rbuf [1], bufferlength );
Or more general:
Move (buffer, rbuf [low (rbuf)], bufferlength );
Yes
From Delphi help keyword string to pchar Conversions
Converting long strings to pchar is not automatic. The differences between them lead to problems in their conversion.
1. long strings are referenced for counting, while pchar is not
2. assign a value to a long string to copy data, while pchar is a pointer to Data.
3. The long string ends with a null separator and contains the length of the string. Pchar is the end of a simple null Terminator. (The end of an empty separator is #0)
Procedure my_func (X: string );
Begin
Some_proc (pchar (x); // refer to the first article, so that you are responsible for the lifetime of X.
End;
Function title (N: integer): pchar;
Var
S: string;
Begin
S: = Format ('title-% d', [n]);
Result: = PChar (s); // refer to 2nd. This is not the case.
End;
Differences between String (also called LongString and AnsiString) in Delphi and traditional PChar
1. string is supported internally by the Delphi compiler (predefined or built-in) and is a basic data type of Delphi, while PChar is just a pointer to a zero-terminated string;
2. the String stored strings are allocated memory in Heap. THE String variable is actually a pointer to the zero-terminated String, and it also has the reference count function, and saves the string length. When the reference count is zero, the occupied space is automatically released.
3. assign a string value to another string. It is just a simple pointer value, which does not generate a copy action, but adds the string reference count;
4. Assigning a PChar variable type to a string variable type will generate a real Copy action, that is, copy the entire string pointed to by PChar to the memory allocated for the string;
5. assign the string value to a PChar variable type, but simply assign the string pointer value to the PChar variable type, and the string reference count does not change as a result of this operation, in this case, PChar depends on the string. When the string reference count is zero and the memory space is automatically released, PChar may point to an invalid memory address, in your program, you must be careful with this situation.
6. PChar is much faster than string, but PChar is a lagging way to manage strings, while string wins with efficient management, PChar only exists to be compatible with earlier types and operating systems (often used when calling Windows APIs). We recommend that you use string.
I think your demo program is very poorly designed and basically cannot be explained:
Demo program I designed:
Procedure TForm1.Button1Click (Sender: TObject );
Var
P: PChar;
S1, S2: string;
Begin
S1: = '1234567abcde ';
S2: = S1;
P: = PChar (S1 );
ShowMessage (IntToStr (SizeOf (S1); // obtain the size of the S1 variable
ShowMessage (IntToStr (integer (S1); // obtain the pointer address of the string pointed to by S1
ShowMessage (IntToStr (integer (P); // obtain the pointer address of the string pointed to by P
ShowMessage (IntToStr (integer (S2); // obtain the pointer address of the string pointed to by S2
S1: = P;
ShowMessage (IntToStr (integer (S1 )));
ShowMessage (IntToStr (integer (P )));
ShowMessage (IntToStr (integer (S2 )));
End;
Key Points of code analysis:
S2: = S1 verify my summary of 3rd points
P: = PChar (S1) Verify the 5th points I have summarized
S1: = P verify my summary of 4th points
The results shown here in order (your results may be different, but do not affect the analysis)
4 <= S1 variable size
4530660 <= string pointer address pointed to by S1
4530660 <= pointer address of the string pointed to by P
4530660 <= string pointer address pointed to by S2
9780484 <= the pointer address of the string to which S1 points (address changed)
4530660 <= pointer address of the string pointed to by P
4530660 <= string pointer address pointed to by S2
I don't think it is necessary to analyze its assembly code. First, let's figure out these basic operations!
If you do not consider too much, we adopt a simple method:
1: pchar to string:
Ansistring;
Pchar B;
A = string (B );
2: string to pchar
Ansistring;
Pchar B;
B = pchar ();
However, the type is not safe.

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.