Arch of Delphi

xiaoxiao2021-03-05  50

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. Typeta = array [0..9] of integer; ... Vara: Ta; and the following code usually effect is the same (different places in the type of type) VARA: 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 or DIM A (0 to 9) as long below: Multidimensional array: multi-dimensional array The essence is actually an array of arrays. Typeta = 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; TA, TB, TC here are 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 how he applies for it, and it is not necessary to distinguish it. How to get the dimension of the multidimensional array? As mentioned earlier is an array of arrays, so the following methods can be used to achieve multi-dimensional number of dimensional numbers: Vark: Array [2..10, 3..20] of integer; BeginshowMessage (INTOSTR (Low (K) ) '' INTOSTR (HIGH (K))); SHOWMESSAGE (INTSTR (Low (K (K)])) '' INTOSTR (HIGH (K [Low (K)]))); // K [N] is Array [3..20] of integer; an array end; dynamic array: OP in the statement of an array is Typeta = array of integer; a wide range of dynamic array applications. 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 such statements: Vara, B: array [ 0..19] of integer; ... copymemory (@A, @B, 20 * sizeof (integer)); or copymemory (@a [0], @B [0], 20 * sizeof (integer)); It's all right, 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.

It can only be written: Vara, B: Array of Integer; ... setlength (a, 20); setlength (b, 20); copymemory (@a [0], @B [0], 20 * sizeof (Integer) ); 2) Addition of arrays: The added value of the static array is very simple VARA, B: Array [0..19] of integer; ... A: = b; assign array to the array; in the dynamic array Be careful, see Vara, B: Array Of Integer; Beginsetlength (A, 10); SETLENGTH (B, 10); B [1]: = 50; A: = B; B [1]: = 100; What is END; A [1]? 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 TSHORTSTRINGTH: BYTE; DATA: ARRAY [1..LENGTH] of char; end; From the structure you can see that shortstring maximum can only save 255 characters. We can do an experimental Vark: shortstring; begink: = 'i am a delphi fan!'; K [0]: = char (13); // k.length is set to 13ShowMessage (k); end; /// Two ways effects the same Vark: shortstring; p: ^ byte; begink: = 'i am a delphi fan!'; P: = @ K [1]; DEC (p, 1); p ^: = 13; //// K.Length is set to 13ShowMessage (K); END; run, see what results will be, huh, huh. Why is the lower limit of the array of shorts? I want to think about the relationship with Byte and Char. I don't want to say more .2. NanSISTRING: Standard string, ending with NIL. Typetanstructed = Recordallocsiz: longword; // Assignment size Reference: longword: longword; // Quote LENGTH: longword; // String length Data: array [1 .. (length 1)] OF ASICHAR; END; Someone wants to 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 CC compatibility are also very good. As follows: Vark: ansistring; p: ^ longword; begink: = 'i am a delphi fan!'; P: = @ K [1]; DEC (p, 1); p ^: = 13; // k. Length is set to 13ShowMessage (K); end; thereby we know that Ansistring is very efficient, and his information is stored in the character serial.

For example, we have to take the length of the string only read the contents of Tansistring.length. And its CC string is not, the length of the string should be read from the header until the number of characters read under NULL record 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 being 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 code: Vark, J: Ansistring; Begink: '123'; J: = '456'; K: = J; END; K's space has been assigned, since K's pointer points to J, then k The original space is not a waste, causing memory garbage? Ansistring is also one of the dynamic arrays, in line with the dynamic array surplus. 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 size is not very meaningful to us, so we will not take him first, you will pick it up. : Ansistring; p: ^ longword; myanstrument: Pansistring; begink: = 'i love delphi!'; Counter should be 1, because just allocated memory, only one user P: = @ K [1]; DEC (p, 2 ); Myanstring: = @ (p ^); showMessage ('Now counter:' INTOSTR (myanstrung ^ .referencount); showMessage ('length:' INTOSTR (Myanstruing ^ .length); J: = K; // The counter should add 1, because J's pointer points to him, the user has a lot of showMessage ('Now counter:' .referencount); showMessage ('length:' INTOSTR (myansiString ^ .Length)); J: = '123'; // counter should be reduced, because J's pointer finger no longer points to him, users have less ShowMessage ('Now counter:' INTOSTR (myanstrunt ^ .reference)) ShowMessage ('length:' INTOSTR (Myanstring ^ .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)); END; The principle of recycling mechanism of dynamic arrays 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: TypePAnsiString = ^ TAnsiString; TAnsiString = recordReferencCount: LongWord; // Cited Length: LongWord; // string length end; vark, j: array of Integer; P: ^ LongWord; MyAnsiString: PAnsiString; beginSetLength (k, 10 ); / / Counter should be 1, because just allocated memory, only one user Ksetlength (J, 10); P: = @ K [0]; DEC (p, 2); myanstruing: = @ (p ^); SHOWMESSAGE ('Today Counter:' INTOSTR (Myanstring ^ .referencount); showMessage ('length:' INTOSTR (Myanstring ^ .length)); J: = K; / / counter should add 1, because J's pointer Pointing to him, the user has a showMessage ('Now counter:' .referencount); showMessage ('length:' INTOSTR (Myanstruing ^ .length)); setlength (j, 100) ; // The counter should be reduced, because J's pointer finger no longer points to him, the user has less ShowMessage ('Now counter:' .referenccount); showMessage ('length:' INTOSTR 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-36541.html

New Post(0)