Delphi memory operation function (4): clearing and filling the memory

Source: Internet
Author: User
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.

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.