Fillmemory and zeromemory functions are clear at a glance, but they all call fillchar;
To empty a bucket, enter the null character (#0: The character number is 0.
For the following test, first write a function to view the memory in hexadecimal mode:
Function getmembytes (var x; Size: integer): string; var Pb: pbyte; I: integer; begin Pb: = pbyte (x); for I: = 0 to size-1 do begin result: = Result + inttohex (PB ^, 2) + #32; Inc (PB); end; {getmembytes end} // test: var P1: pansichar; P2: pwidechar; S1: ansistring; S2: unicodestring; begin P1: = 'abcd'; P2: = 'abcd '; s1: = 'abcd'; S2: = 'abcd'; showmessage (getmembytes (P1, 4); {41 42 43 44} showmessage (getmembytes (P2, 8 )); {41 00 42 00 43 00 44 00} showmessage (getmembytes (S1, 4); {41 42 43 44} showmessage (getmembytes (S2, 8 )); {41 00 42 00 43 00 44 00} end;
Test the fillmemory, zeromemory, and fillchar filling functions:
Const num = 10; var P: pchar; begin P: = stralloc (Num); showmessage (getmembytes (p, num )); {The result shows that stralloc does not initialize memory} fillmemory (p, num, byte ('A'); showmessage (getmembytes (p, num )); {41 41 41 41 41 41 41 41 41 41} zeromemory (p, num); showmessage (getmembytes (p, num )); {00 00 00 00 00 00 00 00 00} fillchar (P ^, num, 'B'); showmessage (getmembytes (p, num )); {42 42 42 42 42 42 42 42 42 42} strdispose (p); end;
Now, I have a question:
Getmem and getmemory do not initialize the memory; allocmem will initialize the memory to be empty, then
Will reallocmem and reallocmemory initialize the memory?
Test (the result is not initialized ):
{Test 1} var P: pointer; begin P: = getmemory (3); showmessage (getmembytes (p, 3); reallocmem (p, 10 ); showmessage (getmembytes (p, 10); {No initialization} freememory (p); end; {Test 2} var P: pointer; begin P: = allocmem (3 ); showmessage (getmembytes (p, 3); reallocmem (p, 10); showmessage (getmembytes (p, 10); {No initialization} freememory (p); end;
In addition, the operation objects of fillmemory and zeromemory are pointers, while the operation objects of fillchar are entities.