Types and pointers of Delphi [2]

Source: Internet
Author: User
The data in the memory except 0 is 1. You treat it as an image, character, number, and so on. That's your business. The memory only knows 0 and 1.

In addition to hard memory, the Win32 system can also open up virtual memory from the hard disk;

The Win32 memory address range is within 4 GB (0 .. 232-1), so it can be used for one application at most.ProgramAllocate 4 GB of runtime space, and 2 GB of system management, in fact, the program only has 2 GB of autonomous space. Do you remember that the maximum length of string is 2 GB? This is the truth.

With 4 GB memory, there are 4 GB addresses, that is, there can be a maximum of (1024*1024*1024*4-1 = 4294967295) memory addresses, this is the maximum cardinal value in Delphi, so the 32-bit pointer type is always a cardinal number.

A memory address is a number between 0 .. 4294967295. You can read or write data through the memory address;
A pointer is used to index or identify memory. It is also 0 .. A number between 4294967295. Although they are different, you can use pointers to find the memory address of the actually stored data and read and write it according to the specified type.

For example:

VaR STR: string; N: Cardinal; pstr: pstring; begin STR: = 'abcde'; N: = Cardinal (STR); {Get memory address} pstr :=@ STR; {now pstr is the STR pointer} {the result of N and pstr is (the result is random. If you know it is different):} showmessage (inttostr (n )); {4571092} showmessage (inttostr (Cardinal (pstr); {1244652} {but you can find STR} showmessage (pstr ^) through pstr; {ABCDE} end;

  

After the program runs, the memory of the string is basically the following (in bytes). N in the above example marks the position of the substring:

Bytes
A B C D E

For a binary chart:

Bytes
00001010 00001011 00001100 00001101 00001110

If we only look at binary data, it is hard to know what this data is. Besides, why does it have to be a string "ABCDE? This is not certain.

In the following example, we first authorize and treat it as a string, but as the pointer moves, the string is also changing.

Then, the byte pointer (pbyte) and integer pointer (pinteger) are used to read the data respectively, and the corresponding values are also obtained.

Complete example:

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); Procedure button3click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: tobject); var STR: string; PS: pchar; N: Cardinal; begin STR: = 'abcde'; PS: = pchar (STR ); n: = Cardinal (PS); // n: = Cardinal (STR); {This line can replace the above two rows} showmessage (inttostr (n )); {result: showmessage (pchar (n); {ABCDE} showmessage (pchar (n + 1 )); {bcde} showmessage (pchar (n + 2); {cde} showmessage (pchar (N + 3); {de} showmessage (pchar (n + 4 )); {e} end; Procedure tform1.button2click (Sender: tobject); var STR: string; N: Cardinal; Pb: pbyte; begin STR: = 'abcde'; N: = Cardinal (STR); showmessage (inttostr (n); {4571140; this is my result, which is random} Pb: = pbyte (N ); showmessage (inttostr (PB ^); {65} Pb: = pbyte (n + 1); showmessage (inttostr (PB ^); {66} end; procedure tform1.button3click (Sender: tobject); var STR: string; N: Cardinal; pint: pinteger; begin STR: = 'abcde'; N: = Cardinal (STR ); showmessage (inttostr (n); {4571140; this is my result here, which is random} pint: = pinteger (n); showmessage (inttostr (pint ^ )); {1145258561} pint: = pinteger (n + 1); showmessage (inttostr (pint ^); {1162101570} end; end.
 
   
 

The results of the third program section above may confuse you:
The first result should have something to do with "ABCD". Why is it 1145258561?
The second result should have something to do with "bcde". Why is it 1162101570?

Why? Of course this is true. Let me explain:
1145258561 is converted to a hexadecimal value: 44434241, which is clearly written: $44 $43 $42 $41. Do you still remember that intel and other popular CPU Schedule Data is upside down?

Calculate the next one by yourself, and use the calculator in the attachment.

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.