Delphi's Memory manipulation function (2): allocating memory to array pointers

Source: Internet
Author: User

A static array that allocates memory at the time of declaration, such as:

var Array [0.. 255  of Array [0.. 255  of Integer;  begin SHOWMESSAGEFMT (' array size is:%d,%d '{the size of the array is: 512, 1024x768}end, respectively;


For static array pointers, Although there is no memory allocated at the point of declaration, how much memory this pointer should allocate is DETERMINISTIC.

In this case, we should use New and Dispose to allocate and free memory. Such as:

typeTARR1 =Array[0..255] ofChar; TARR2 =Array[0..255] ofInteger;varArr1: ^tarr1; Arr2: ^tarr2;beginNew (arr1);  New (arr2); Arr1^: =' In the event of Delphi blog '; SHOWMESSAGEFMT ('%s%s ', [arr1^[0], arr1^[1]]);{in the event}//showmessagefmt ('%s%s ', [arr1[0], arr1[1]]); {this is also possible}Arr2[low (arr2^)]: = Low (Integer);{the first element assigns a minimum value}Arr2[high (arr2^)]: = MaxInt;{the first element assigns a maximum value}SHOWMESSAGEFMT ('%d,%d ', [arr2[0], arr2[255]]);{ -2147483648, 2147483647}Dispose (arr1); Dispose (arr2);End;//work around and do this again:typeTARR1 =Array[0..255] ofChar; TARR2 =Array[0..255] ofInteger;  PARR1 = ^tarr1; PARR2 = ^tarr2;vararr1:parr1; arr2:parr2;beginNew (arr1);  New (arr2); Arr1^: =' In the event of Delphi blog '; SHOWMESSAGEFMT ('%s%s ', [arr1[0], arr1[1]]);  Arr2[low (arr2^)]: = Low (Integer);  Arr2[high (arr2^)]: = MaxInt; SHOWMESSAGEFMT ('%d,%d ', [arr2[0], arr2[255]]);{ -2147483648, 2147483647}Dispose (arr1); Dispose (arr2);End;


allocating memory to a known-sized pointer should be used with New, and the example above is about a static array pointer, as well as a pointer to the struct (RECORD) to be mentioned Later.

The nature of New is also called getmem, but does not require us to specify the Size.

But this is not appropriate for dynamic arrays, but allocating memory SetLength to dynamic arrays should be sufficient, for example:

   var     arr:   Array     of   Integer;   begin     SetLength (arr, 3 );  arr[0 ]: = Random (100 );   Arr[1 ]: = Random (100 );  arr[< Span style= "color: #0000ff" >2 ]: = Random (100 );  showmessagefmt ( Span style= "color: #0000ff" > '%d,%d,%d ' , [arr[0 ],arr[1 ],arr[2 ]"); {0,3,86}    end  ; 


How do you assign memory to a pointer to a dynamic array? In fact, the dynamic array variable itself is a pointer, do not go around and give it a pointer.

But the idea is that we can convert an untyped pointer to a dynamic array type, such as:

typeTARR =Array  ofInteger;varp:pointer;beginGetmem (p,3* SizeOf (Integer));{allocate a space that can hold 3 integers}{this is the same size as the TARR of 3 elements, but type conversions are required for use}TARR (p) [0]: = Random ( -); TARR (p) [1]: = Random ( -); TARR (p) [2]: = Random ( -); SHOWMESSAGEFMT ('%d,%d,%d ', [TARR (p) [0], TARR (p) [1], TARR (p) [2]]);{0,3,86}Freemem (p);End;


The Getmem and Freemem are used here, which is more commonly used for assigning untyped pointers. For other types of pointers it is possible, but not necessarily the best scenario, such as:

//get the window caption (obviously better than the Stralloc mentioned Earlier) var p:pointer; begin  the  the  {Form1} Freemem (p); End;


GetMemory and freememory should be advocated instead of Getmem and freemem, for example:

var p:pointer; begin P: = getmemory (Form1} freememory (p);  End;


First summarize the Following:
New is allocating memory to a known-sized pointer;
The getmem mainly allocates memory to the untyped pointer;
Try to use getmemory instead of Getmem.

What's the difference between a allocmem and them?

AllocMem allocates memory, It is initialized (empty) at the same time, and Getmem does not, first verify:

var p1,p2:pointer;  begin P1: = AllocMem (The null is shown here} freememory (p1); P2: = GetMemory (Thiswill show some junk data, depending on the contents of the previous address before allocation} freememory (p2);  End;


About the difference between freememory and freemem:
1, Freememory will check whether it is nil again freemem, this is a bit similar: free and Destroy;
2, Freemem also has a default parameter can specify the size of the memory to be freed, without specifying the full release (no need to release only part of it);
3, the New corresponding Dispose can also be replaced with FREEMEM or freememory.

Try to use Freememory to release memory allocated by getmem, getmemory, allocmem, reallocmem, reallocmemory.

reallocmem, Reallocmemory is allocated memory on the basis of the reallocation of memory, it is almost reallocmemory more than reallocmem a nil judgment, try to use reallocmemory bar. for example:

typeTARR =Array[0.. maxlistsize] ofChar; PARR = ^tarr;vararr:parr; i:integer;beginArr: = GetMemory (5); forI: =0  to 4  doArr[i]: = CHR ( $+i); ShowMessage (PChar (arr));{ABCDE}Arr: = Reallocmemory (arr, -); ShowMessage (PChar (arr));{ABCDE} forI: =0  to  -  doArr[i]: = CHR ( $+i); ShowMessage (PChar (arr));{abcdefghijklmnopqrstuvwxyz}End;


Note the TARR type in the above example, which is defined as a large enough array; This array leaves enough possibilities, but it is not generally used.
We generally only use pointers to this array, otherwise the initialization will be out of memory.
Even if the pointer is used, it cannot be initialized with the New row; Should use getmem, getmemory, allocmem, reallocmem, reallocmemory and so on how many applications.
It is important to note that the reallocation of memory may also be less forewarned; If the forewarned is larger, it should be able to guarantee the existence of the previous Data.
This is the idea used in the TList class in VCL.

If you can't accept that large array in your heart (it's okay, How big is a pointer?), or you can do this:

typeTARR =Array[0..0] ofChar; PARR = ^tarr;vararr:parr; i:integer;beginArr: = GetMemory (5); forI: =0  to 4  doArr[i]: = CHR ( $+i); ShowMessage (PChar (arr));{ABCDE}Arr: = Reallocmemory (arr, -); ShowMessage (PChar (arr));{ABCDE} forI: =0  to  -  doArr[i]: = CHR ( $+i); ShowMessage (PChar (arr));{abcdefghijklmnopqrstuvwxyz}End;


This seems to be confusing, only an array of elements can do?
It should be understood that only this element is sufficient to indicate the starting point of the data and the size and regularity of the data elements.

The other sysgetmem, sysfreemem, sysallocmem, sysreallocmem Four functions, should be the underlying implementation of these functions, in the case of using the Delphi default memory manager, We still do not use them directly.

Delphi's Memory manipulation function (2): allocating memory to array pointers

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.