Delphi's Memory manipulation function (1): allocating memory to the character pointer (PChar, Pwidechar, Pansichar) the best choice for allocating memory is stralloc. The string is initialized when the memory is allocated)

Source: Internet
Author: User

The functions that can be thought of right now are:

Getmemallocmemreallocmemfreememgetmemoryreallocmemoryfreememorynewdisposenewstrdisposestrstrnewstrallocstrdisposeglobalal Locptrglobalfreeptrwidestrallocansistrallocstrdisposemovemovememorycopymemoryzeromemoryfillmemoryfillcharstrbufsize


assigning memory to character pointers (PChar, Pwidechar, Pansichar) is the best option: Stralloc.

Stralloc, although eventually called Getmem, Stralloc will add 4 management bytes (record length) required by Delphi in front of the pointer.

Stralloc allocated memory, released with Strdispose, with Strbufsize to get the size.

Can you release it with Freemem? This frees up 4 bytes less.

This type of pointer is typically used for parameters of API functions, such as getting the window caption:

var p:pchar; begin P: = Stralloc (Form1} strdispose (p);  End;


Stralloc calls Widestralloc and Ansistralloc according to different parameters (Pwidechar, Pansichar), so we can also use these two functions directly (which also need to be released by Strdispose). But there is little need to use them; Specify a good parameter type with Stralloc.

assigning memory to a character pointer other methods are also quite handy, such as:

//Get the directory where WINDOWS is located var Array [0 of Char; begin {C:\WINDOWS} End;


The memory of the array is not our own application, the system will be automatically released; Remember: You must manually release the memory as long as it is manually requested.

We request memory for the character pointer mainly to accept the data in the API, if we want to assign the constant value directly, the system will automatically allocate memory, for example:

var p:pchar; begin ' In the event of Delphi blog ' {In the event of Delphi blog} End;


Of course, we can also use this method to apply for memory, is stupid point, for example:

//Get system directory var p:pchar; begin P: = PChar (Stringofchar (Char (0){Repeat a null character 256 times into a string and then convert to PChar}{c \ Windows\System32}end;


If the character pointers required in the API function are for input, then of course there is no need to request memory, for example:

//Set window titlevarP:pchar;beginP: =' window new title '; SetWindowText (Handle, p);End;//can also be given directly to constantsbeginMessageBox (Handle,' hint message ',' title ', MB_OK);End;//If it is a variable or constant to a string, you need to convertvarStr:string;beginSTR: =' In the event of Delphi blog '; TextOut (Canvas.handle,Ten,Ten, PChar (str), Length (str));{output text on a form, this code cannot be in the OnCreate event}End;


Off the topic ... Stralloc, Strdispose, Widestralloc, Ansistralloc, and strbufsize have been used in several functions.

There are Newstr, Disposestr, strnew, Strdispose also seem to have a relationship.

First say Newstr and DISPOSESTR (they are a pair);
NEWSTR is based on the ansistring to create a new pansistring, but this is for compatibility exists, Delphi has not advocated the use of.
The functions that are no longer advocated are prefixed with deprecated and are shown in gray in code hints.
You can actually get the string pointers with @, but you don't need them at all.

There is a strnew; Strnew can be made with the same character pointer, for example:

var p1,p2:pchar; begin  ' Delphi ';  P2: = Strnew (p1);  SHOWMESSAGEFMT ('%s,%s '{delphi, Delphi}'"; SHOWMESSAGEFMT ('%s,%s '{{), Delphi}{releases its own application}end;


But the meaning of strnew is not very significant, we can do it more simply:

var p1,p2:pchar; begin  ' Delphi ';  P2: = p1;  SHOWMESSAGEFMT ('%s,%s '{delphi, Delphi}'"; SHOWMESSAGEFMT ('%s,%s '{{), Delphi}end;


After all, it seems that only stralloc is worthy of our memory.

There are also a couple of very important related functions: Globalallocptr, globalfreeptr; Their function is above these are irreplaceable!

Globalallocptr and Globalfreeptr are the simplification of system functions: GlobalAlloc, GlobalFree, because they are important, just because they can operate across processes; Globalallocptr, however, allocates memory to an untyped pointer (Pointer), but not just for character pointers. Let's go back to the next topic and do some more examples.

Http://www.cnblogs.com/del/archive/2008/11/08/1329543.html

Delphi's Memory manipulation function (1): allocating memory to the character pointer (PChar, Pwidechar, Pansichar) the best choice for allocating memory is stralloc. The string is initialized when the memory is allocated)

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.