Arch of Delphi

zhaozj2021-02-08  234

A rich data type can be established in the Object Pascal of Delphi. An array is no doubt is also one of many custom data types. Type ta = array [0..9] of integer; ... var A: Ta; and the following code usually effect is the same (different places in the type of type) VAR A: array [0..9] OF INTEGER; this is equivalent to INT A [10] in C; or DIM A (9) AS Long or Dim A (0 to 9) as long below in Basic: multi-dimensional array: The essence of the multi-dimensional array is actually an array of arrays. TYPE TA = Array [0..9] of array [0..9] of integer; TB = array [0..9, 0..9] of integer; TCA = array [0..9] of integer; Tc = array [0..9] of TCA; here TA, TB, TC is equivalent. There is no separate in use. For example, X [0] [0]: = 1; TX [0, 0]: = 1; (x is TA, TB, TC type) is legal. Because these types have opened up a 100X100XSIZEOF (Integer) area in memory, you can't distinguish between how he applied, and there is no need to distinguish. How to get the dimension of the multidimensional array? As mentioned earlier is an array of arrays, so you can use the following methods to achieve multi-dimensional number of dimensional numbers: VAR K: Array [2..10, 3..20] of integer; begin showmessage (INTOSTR (Low) K)) '' INTOSTR (HIGH (K))); ShowMessage (INTSTR (Low (K (K)])) '' INTOSTR (HIGH (K [Low (K)]))))) // k [n] is array [3..20] of integer; array

END; Dynamic array: OP in the statement of an array of states is Type TA = Array Of Integer; the dynamic array application is very broad. There is now a trend is that in the data structure instead of the linked list (which is good, which bad self-evaluation we are not discussed here). Maybe you will say that the dynamic array does not have to use it, I have never used it. I don't believe you have not used a dynamic array! String Type You used it, it approximes it to a dynamic array. The memory of the dynamic array is assigned when using the allocation length. His downlord can only be 0 (ansistring string exceptions, the lower bound is 1, and then the reason is later). The live array is generally not released after the use of self-management. If you want to force it to give him NIL. Using a dynamic array often loves the mistake: 1) and static array concept confusion: The address of the dynamic array is not the address of his first element, and in a static array, we often use this statement: Var A, B: array [0..19] of integer; ... copymemory (@a, @B, 20 * sizeof (integer)); or copymemory (@a [0], @B [0], 20 * sizeof (integer)) ; All is correct, because the first address of the array in static arrays is the address of his first element.

However, only the second write method will get the correct result in the dynamic array. That is, you can only write: VAR A, B: Array of Integer; ... setlength (a, 20); setlength (b, 20); copymemory (@a [0], @B [0], 20 * Sizeof (Integer) ); 2) Names of arrays: Names in static arrays VAR A, B: Array [0..19] of integer; ... A: = B; assign arguments to array; Be careful in arrays, see VAR A, B: Array Of Integer; Begin SetLength (A, 10); SetLength (B, 10); B [1]: = 50; A: = B; B [1] : = 100; END; A [1] What is it? According to the common sense A [1], it should be the value before the value, but the value of the opposite of A [1] is the value of B [1], the value is again assigned, 100 dynamic arrays A: = B; = B; = B; The pointer to the dynamic array A points to the dynamic array B, which is not to open up a space as we want. If you do not open a space for A, you must use COPY to copy the data of B. Ansistring is also a dynamic array, so the same situation also exists in the String type. Special arrays: The characters are called separately, but they are not alone because it has too many dynamic array characteristics. Again, the code here and you are likely to be used, but you can deepen your understanding of ObjectPascal. When you design, you will know how it works, through these you will use it more efficiently. Most of the strings say that it is better to say that he is a structure. But we use his array characteristics more. 1.shorstring: It is the type that is reserved with old Pascal compatibility. Maximum 255 characters. TYPE TSHORTSTRING Length: Byte; Data: Array [1..LENGTH] of char; end; From the structure you can see that ShortString can only save 255 characters. We can do an experimental VAR K: shortstring; begin k: = 'i am a delphi fan!'; K [0]: = char (13); // k.length is set to 13 showMessage (k); end; /// The two method effects are the same var k: shortstring; p: ^ Byte; begin k: = 'i am a delphi fan!'; P: = @ K [1]; DEC (p, 1); p ^: = 13; // k.length is placed to 13 showMessage (k); END; run, see what results will come, huh, huh. Why is the lower limit of the number of shorts? I want to think about the relationship with Byte and Char. I don't want to say more. 2. Guistring: Standard strings, end with NIL.

TYPE TANSISTRING = Record Allocsiz: longword; // Assignment size refrest: longword; // Reference number length length: longword; // String length DATA: array [1 .. (length 1)] OF ASICHAR; END; Someone Ask why the length is 1 .. (Length 1) instead of Length? Because there is a NULL character behind, it is ended. It can be seen that Ansistring and C / C are also very good. As follows: VAR K: ANSISTRING; P: ^ longword; begin K: = 'I am a delphi fan!'; P: = @ K [1]; DEC (p, 1); p ^: = 13; // K.Length is set to 13 ShowMessage (K); END; thereby we know that Ansistring is very efficient, and his information is stored in the character. For example, we have to take the length of the string only read the contents of Tansistring.length. The string of C / C is not line, and the length of the string should be read from the header until the number of characters read under NULL records is the length. However, if the string is very efficient, it is displayed, such as a big file? It is said that the dynamic array assignment problem is not a single space for a dynamic array, but a simple point to point the array. Only the array assigned is really assigned to him when changing. The benefits of this will be very fast when assigning values. If the array being assigned is not changed, it is much better than the direct allocation space. In addition, if you change the array, change the reassignment space, just spend the same time as the space is allocated. Some people will think so as follows: Var K, J: Ansistring; Begin K: '123'; J: = '456'; K: = J; END; K's space has been assigned, since K's pointer points to J, So the original space is not a waste, causing memory garbage? Ansistring is also a kind of dynamic array, in line with dynamic array survival. However, the actual situation is: When a pointer points to a character invasive space, his reference number is added 1, and when the pointer points to the reference number, the number of references will be reduced by 1, so when the reference number is 0 It means that no one will use it to automatically release this space. This is the self-management technology we said.

Type // The actual distribution is not very meaningful to us, so we don't pick him up here first. Pansusetring = ^ tanstring; tanstring = record refrest: longword; // Reference number length: longword; // String length End; var k , J: ANSISTRING; MYANSISTRING: PANSISTRING; Begin K: = 'i love delphi!'; counter should be 1, because just allocated memory, only one user P: = @ K [1]; DEC P, 2); myanstrument: = @ (p ^); showMessage ('Today's Counter:' INTSTOSTR (Myanstring ^ .referencount); showMessage ('length:' INTOSTR (MyansString ^ .length); J: = K; // counter should add 1, because J's pointer points to him, the user has more showMessage ('Now counter:' .reference); showMessage ('length:' INTOSTR (Myanstring ^ .length); J: = '123'; // The counter should be reduced, because J's pointer does not point to him, the user has a showMessage ('Now counter:' INTOSTR (Myanstruing ^. Referenccount); showMessage ('length:' INTOSTR (MyansString ^ .length)); K: = J; // K's pointer points to J's content, then the counter of this data is subtracted again, that is 0. // However, if the memory area has been automatically released, it is some random data showMessage ('Today's Counter:' INTOSTR (Myanstruing ^ .referencount); showMessage ('length:' INTOSTR (myanstruing ^ .Length));

The principle of the recycling mechanism of the dynamic array is the same, and now I understand why the dynamic array is reclaimed to give him NIL! The type of survival period such as variant type is achieved in this mechanism. Why is a string of dynamic arrays? Because the above methods are equally applicable to dynamic arrays. We use the dynamic array to realize an ANSISTRING to see if it doesn't work? For example: Type PAnsiString = ^ TAnsiString; TAnsiString = record ReferencCount: LongWord; // Cited Length: LongWord; // string length end; var k, j: array of Integer; P: ^ LongWord; MyAnsiString: PAnsiString; begin SetLength (k, 10); // counter should be 1, because just allocated memory, only one user K setLength (J, 10); P: = @ K [0]; DEC (p, 2); myanstruing: = @ (p ^); showMessage ('Today):' Because J's pointer points to him, the user has more showMessage ('Now counter:' .referencount); showMessage ('length:' INTOSTR (Myanstruing ^ .length); setlength (j, 100); // The counter should be reduced, because J's pointer refers to him again, the user has less ShowMessage ('Now counter:' INTOSTR (myanstrung ^ .referencount); showMessage ('length is : ' INTOSTOSTR (Myanstring ^ .length); setlength (k, 100); // k memory is reassigned, then the counter of this data is subtracted again, that is, 0. // However, if there is no user, the memory area has been automatically released, it is some random data showMessage ('Now counter:' INTOSTR (Myanstring ^ .reference)); showMessage ('length:' INTOSTR (Myanstring ^ .length)); END; It is theoretically to simulate some of the functions of Ansistring, which can see Ansistring is just a form of dynamic arrays.

转载请注明原文地址:https://www.9cbs.com/read-2880.html

New Post(0)