Memory Processing Methods of system units
1. Move-move memory block
VaR
Source, DEST: string;
Begin
Source: = '000000 ';
DeST: = '---------';
Move (source [5], Dest [3], 4 );
Showmessage (DEST); {-- 5678 ---}
End;
2. system. New, system. Dispose-apply for and release memory for a pointer
Type
Tmyrec = record {definition structure}
Name: String [8];
Age: word;
End;
Pmyrec = ^ tmyrec; {define the structure pointer; Delphi generally defines it before the structure, all can}
If the structure is used directly, no pointer is used}
Procedure tform1.button1click (Sender: tobject );
VaR
R: tmyrec;
Begin
R. Name: = 'zhang san ';
R. Age: = 99;
Showmessage (format ('% S % d', [R. Name, R. Age]); {Zhang San 99 years old}
End;
{If structure pointer is used}
Procedure tform1.button2click (Sender: tobject );
VaR
PR: pmyrec;
Begin
New (PR); {memory allocation}
PR ^. Name: = 'zhang san ';
PR ^. Age: = 99;
Showmessage (format ('% S % d', [pr ^. Name, PR ^. Age]); {Zhang San 99 years old}
Dispose (PR); {release memory}
End;
{Delphi allows the following simplification when using the structure}
Procedure tform1.button3click (Sender: tobject );
VaR
PR: pmyrec;
Begin
New (PR );
Pr. Name: = 'zhang san ';
Pr. Age: = 99;
Showmessage (format ('% S % d', [pr. Name, PR. Age]); {Zhang San 99 years old}
Dispose (PR );
End;
3. system. getmem, system. freemem-apply for and release memory
If only one pointer is allocated memory, it is the same as new and dispose. The difference is that getmem can apply for multiple consecutive memory blocks.
Type
Tmyrec = record {definition structure}
Name: String [8];
Age: word;
End;
Pmyrec = ^ tmyrec; {define structure pointer}
VaR
PR: pmyrec;
Procedure tform1.formcreate (Sender: tobject );
Begin
{Allocate three structures of memory at the same time; the three structures in the memory are sequential}
Getmem (PR, sizeof (tmyrec) * 3 );
{At this time, PR points to the first structure and assigns a value}
Pr. Name: = 'zhang san ';
Pr. Age: = 11;
{Point PR to the second structure and assign a value}
INC (PR );
Pr. Name: = 'Li si ';
Pr. Age: = 22;
{Point PR to the third structure and assign a value}
INC (PR );
Pr. Name: = 'wang 5 ';
Pr. Age: = 33;
{Return to the first structure and then read it in sequence}
Dec (PR, 2 );
Showmessage (format ('% S % d', [pr. Name, PR. Age]); {Zhang San 11 years old}
INC (PR );
Showmessage (format ('% S % d', [pr. Name, PR. Age]); {Li Si 22 years old}
INC (PR );
Showmessage (format ('% S % d', [pr. Name, PR. Age]); {Wang Wu 33 years old}
{From the first structure, clear the memory of the three structures, so that the above three structures are deleted}
Dec (PR, 2 );
Freemem (PR, sizeof (PR ^) * 3); {sizeof (tmyrec) and sizeof (PR ^) are the same}
End;
4. system. reallocmem-re-apply for memory
Type
Tmyrec = record {define the structure; remember that the size of this structure is 12 bytes}
Name: String [8];
Age: word; {although word is 2 bytes in size, it occupies 4 bytes because it is 4 bytes aligned}
End;
Pmyrec = ^ tmyrec; {define structure pointer}
VaR
PR: pmyrec;
Procedure tform1.formcreate (Sender: tobject );
Const
STR = 'address: % d; Name: % s ';
Begin
{Apply for three tmyrec memory structures}
// Getmem (PR, sizeof (tmyrec) * 3 );
Reallocmem (PR, sizeof (tmyrec) * 3); {This sentence can also be replaced by a previous line}
{Assignment}
Pr. Name: = 'zhang san ';
Pr. Age: = 11;
INC (PR );
Pr. Name: = 'Li si ';
Pr. Age: = 22;
INC (PR );
Pr. Name: = 'wang 5 ';
Pr. Age: = 33;
{Display the addresses and information of the three structures; the addresses should be consecutive (12 bytes )}
Dec (PR, 2 );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 15278504; Name: James}
INC (PR );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 15278516; Name: Li Si}
INC (PR );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 15278528; Name: Wang Wu}
{Re-apply for memory. Five structure sizes are required, and two new structures are assigned a value}
Dec (PR, 2 );
Reallocmem (PR, sizeof (tmyrec) * 5 );
INC (PR, 3 );
Pr. Name: = 'ma 6 ';
Pr. Age: = 44;
INC (PR );
Pr. Name: = 'Sun 7 ';
Pr. Age: = 55;
{Display the relevant information. The address is still consecutive, but it is different from the above !}
Dec (PR, 4 );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 14875920; Name: James}
INC (PR );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 14875932; Name: Li Si}
INC (PR );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 14875944; Name: Wang Wu}
INC (PR );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 14875956; Name: Ma 6}
INC (PR );
Showmessage (format (STR, [INTEGER (PR), Pr. Name]); {address: 14875968; Name: Sun Qi}
Dec (PR, 4 );
Freemem (PR, sizeof (tmyrec) * 5); {You can also use freemem to clear the memory applied by reallocmem}
End;