Dynamic array introduction ---- Delphi
Since there is a dynamic array, the linked list has rarely been used in actual programming in addition to the textbook, and the fact is true, and the array is indeed much faster than traditional linkers, and more convenient.
From Delphi4, it has started the various types of dynamic array support. However, for us, the dynamic array support seems to be not thorough enough, because Delphi does not provide, insert, and moving continuous elements, it is not enough to use it, it is not enough to use it! ! ! J. As a programmer, we must certainly have its own ability to solve the problem, let us briefly introduce the dynamic arrays under Delphi.
In Delphi, the array type has a static array (A: array [0..1024] of integer, dynamic array (VAR A: Array Of Integer), pointer array (ie, pointers pointing to static arrays) and an array (only Parameter passed). Static arrays, the pointer has a fast advantage, and the dynamic array has the advantage of size, the trade-off, that is, the defined dynamic array is converted to a pointer when necessary.
After the dynamic array declaration, only the following functions are available:
1. Set the array size, can be arbitrarily reduced or increased in size
Procedure setLength (Var S; Newlength: Integer);
2. Remove continuous elements to copy to another array variable
Function Copy (S; Index, Count: Integer): array;
3. Number of array and upper and lower limits
Function Length (s): integer;
Function high (x): integer;
Function Low (x): integer;
It is worth noting that the dynamic array that does not add Const or VAR modified will be delivered as a meticulum, and the dynamic array use const modification does not mean that you can't modify the elements in the array (you can try yourself in the program. Another point is that the high function calls the Length function, so we'd better use the Length (s) function when getting the array.
Dynamic arrays occupy 4 bytes in memory space. The allocation table in the memory array is as follows:
Offset content
-8 32-bit reference count
-4 32-bit array length
0 .. Array length * (Element size) - 1 array element element size = sizeof (element type)
According to the above allocation, you can get the following results:
If we want to empty a dynamic array only needs to empty the "array length" and "reference count". "The reference to the above sentence is:" Under the trade-off, it has a compromise, that is, the defined dynamic array is converted to a pointer when necessary. "Below is a function of emptying the dynamic array:
Procedure DynaRraySetzero (VAR A);
VAR
P: plongint; // Take up 4 bytes, just in line with 32-bit memory arrangements
Begin
P: = plongint (a); // pointing to the address of a
DEC (P); // P address offset is SizeOf (A), pointing to array length
P ^: = 0; // Length Clear
Dec (p); / / point to reference count
P ^: = 0; // count the empty.
END;
The above functions are as simple, and the efficiency is also very high.
Let's take a look at how to delete the elements in the dynamic array, the function body is as follows: {******************************* *****
A variable type, elsize = sizeof (a)
Index starts deleting location index, count deleted quantity
*************************************************
Procedure DynaRrayDelete (VAR A; Elsize: longint; index, count: integer;
VAR
Len, MaxDelete: integer;
P: plongint; // 4 bytes of long-intementing pointers
Begin
P: = Plongint (a); // Take the address
IF p = nil dam
EXIT;
{
The following sentence is completely equivalent to DEC (P); len: = p ^ because DEC (P) = PCHAR (P) - 4 is also the shift of 4 bytes, but the latter is moved by bytes}
Len: = plongint (PCHAR (P) - 4) ^; // Variable length, offset-4
If index> = len then // The location to delete is out of range, exit
EXIT;
Maxdelete: = len - index; // Up to the number of deletions
Count: = min (count, maxdelete); // get a smaller value
if count = 0 THEN // does not require deletion
EXIT;
Dec (len, count); // Move to the location you want to delete
MoveMemory (PCHAR (P) Index * Elsize, PCHAR (P) (INDEX Count) * Elsize, (Len-index) * elsize; // Mobile Memory
Dec (p); // remove "array length" position
Dec (p); // remove "reference count" position
/ / Re-allocate adjustment memory, LEN new length. Sizeof (longint) * 2 = 2 * DEC (P)
Reallocmem (p, len * elsize sizeof (longint) * 2);
INC (P); // Point to array length
P ^: = le; // new length
INC (P); // Point to array elements, starting position
Plongint (a): = P;
END;
For the example above, we need to pay attention to the elsize parameter, it must be sizeof (dyarray_name), indicating the number of bytes occupied by the element.
I believe it after reading the above example, for the copy of the dynamic array, movement can also be implemented by it.
Subsequent:
In fact, Delphi is similar to many types of memory allocations, such as String types, in fact, it is very similar to the dynamic array, we can use it as a dynamic array. Since string is a simple version of PCHAR. Anyway, understanding some memory allocation has some benefits to our developers.