Delphi memory operation function (2): allocate memory to array pointers

Source: Internet
Author: User
Static arrays are allocated with memory when declared, for example:
 
VaR arr1: array [0 .. 255] of char; arr2: array [0 .. 255] of integer; begin showmessagefmt ('array sizes: % d, % d', [sizeof (arr1), sizeof (arr2)]); {the array sizes are: 512, 1024} end;

  

For static array pointers, although no memory is allocated at the declaration, the amount of memory allocated by this pointer is fixed.

In this case, we should use new and dispose to allocate and release memory. For example:

 type tarr1 = array [0 .. 255] of char; tarr2 = array [0 .. 255] of integer; var arr1: ^ tarr1; arr2: ^ tarr2; begin new (arr1); New (arr2); arr1 ^: = 'in case of Delphi blog '; showmessagefmt ('% S % s', [arr1 ^ [0], arr1 ^ [1]); {In case} // showmessagefmt (' % S % s ', [arr1 [0], arr1 [1]); {This can also be} arr2 [low (arr2 ^)]: = low (integer ); {minimum value assigned to the first element} arr2 [high (arr2 ^)]: = maxint; {maximum value assigned to the first element} showmessagefmt ('% d, % d ', [arr2 [0], arr2 [255]); {-2147483648,214 7483647} dispose (arr1); dispose (arr2); end; // modify it, in this example, type tarr1 = array [0 .. 255] of char; tarr2 = array [0 .. 255] of integer; parr1 = ^ tarr1; parr2 = ^ tarr2; var arr1: parr1; arr2: parr2; begin new (arr1); New (arr2); arr1 ^: = 'delphi blog in case '; showmessagefmt (' % S % s', [arr1 [0], arr1 [1]); arr2 [low (arr2 ^)]: = low (integer); arr2 [high (arr2 ^)]: = maxint; showmessagefmt ('% d, % d', [arr2 [0], arr2 [255]); {-2147483648,214 7483647} dispose (arr1); dispose (arr2); end;

   

To allocate memory to pointers of known sizes, we should use new. The above example is about static array pointers. The same is true for the struct (record) Pointers mentioned later.

The new function also calls getmem, but we do not need to specify the size.

But this is not suitable for dynamic arrays, but it should be enough to allocate the memory setlength to the dynamic array, for example:

 
VaR arr: array of integer; begin setlength (ARR, 3); arr [0]: = random (100); arr [1]: = random (100 ); arr [2]: = random (100); showmessagefmt ('% d, % d, % d', [arr [0], arr [1], arr [2]); {0, 3, 86} end;

  

How can we allocate memory to pointers to dynamic arrays? In fact, the dynamic array variable itself is a pointer, so don't wrap it around and give it a pointer.

However, the idea is that we can convert a non-type pointer to a dynamic array type, for example:

 
Type Tarr = array of integer; var P: pointer; begin getmem (p, 3 * sizeof (integer )); {space allocated to accommodate 3 integers} {This is the same size as the Tarr of the 3 elements, but type conversion is required.} Tarr (p) [0]: = random (100); Tarr (p) [1]: = random (100); Tarr (p) [2]: = random (100); showmessagefmt ('% d, % d, % d', [Tarr (p) [0], Tarr (p) [1], Tarr (p) [2]); {0, 3, 86} freemem (p); end;

  

Getmem and freemem are used here. It is quite common to allocate a non-type pointer. It can be a pointer of other types, but it is not necessarily the best solution, for example:

 
// Obtain the window title (it is obviously better to use stralloc) var P: pointer; begin getmem (p, 256); getwindowtext (handle, P, 256 ); showmessage (pchar (p); {form1} freemem (p); end;

  

Getmemory and freememory should be recommended to replace getmem and freemem, for example:

 
VaR P: pointer; begin P: = getmemory (256); getwindowtext (handle, P, 256); showmessage (pchar (p); {form1} freemory (P ); end;

  

Summary:
New is to allocate memory to pointers of known sizes;
Getmem mainly allocates memory to non-type pointers;
Use getmemory instead of getmem.

What is the difference between allocmem and allocmem?

Allocmem will be initialized at the same time after memory allocation (empty), and getmem will not. First, verify the following:

VaR p1, p2: pointer; begin P1: = allocmem (256); showmessage (pchar (P1); {null is displayed here} freememory (P1); P2: = getmemory (256); showmessage (pchar (P2); {some junk data is displayed here, depending on the content of the address before allocation} freememory (P2); end;

  

Differences between freememory and freemem:
1. freememory checks whether it is nil and then freemem. This is a bit similar: free and destroy;
2. freemem also has a default parameter that specifies the size of the memory to be released. If this parameter is not specified, all resources will be released (you do not need to release only a part of the memory );
3. The dispose corresponding to new can also be replaced by freemem or freememory.

Use freemory whenever possible to release the memory allocated by getmem, getmemory, allocmem, reallocmem, and reallocmemory.

Reallocmem and reallocmemory are re-allocated based on the allocated memory. The two reallocmemory and reallocmem have more nil judgment than reallocmem. Use reallocmemory whenever possible. For example:

Type Tarr = array [0 .. maxlistsize] of char; Parr = ^ Tarr; var arr: Parr; I: integer; begin arr: = getmemory (5); for I: = 0 to 4 Do arr [I]: = CHR (65 + I); showmessage (pchar (ARR); {ABCDE} arr: = reallocmemory (ARR, 26 ); showmessage (pchar (ARR); {ABCDE} For I: = 0 to 25 do arr [I]: = CHR (65 + I); showmessage (pchar (ARR )); {abcdefghijklmnopqrstuvwxyz} end;

  

Note that in the preceding example, the Tarr type is defined as an array that is large enough. This array sets aside sufficient possibilities, but is generally not used in all cases.
We generally only use the pointer to this array, otherwise the memory will be insufficient during initialization.
Even if its pointer is used, the new row cannot be used for initialization. The getmem, getmemory, allocmem, reallocmem, and reallocmemory values should be used to apply for initialization.
It should be noted that the re-allocation of memory may also result in a smaller score. The larger the score, the larger the score should ensure the existence of the previous data.
This concept is used in the tlist class in VCL.

If you cannot accept such a large array in your mind (in fact, it's okay, a pointer is too big? We only use its pointer), you can also do this:

Type Tarr = array [0 .. 0] of char; Parr = ^ Tarr; var arr: Parr; I: integer; begin arr: = getmemory (5); for I: = 0 to 4 Do arr [I]: = CHR (65 + I); showmessage (pchar (ARR); {ABCDE} arr: = reallocmemory (ARR, 26 ); showmessage (pchar (ARR); {ABCDE} For I: = 0 to 25 do arr [I]: = CHR (65 + I); showmessage (pchar (ARR )); {abcdefghijklmnopqrstuvwxyz} end;

  

This seems confusing. What can an array with only one element do?
It should be understood as follows: This element is enough to indicate the starting point of data and the size and regularity of data elements.

In addition, the four functions sysgetmem, sysfreemem, sysallocmem, and sysreallocmem should be the underlying implementation of these functions. We should not directly use them when using the default Memory Manager of Delphi.

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.