About declaring and resizing two-dimensional arrays in Delphi (for non-basic types of data, beware of memory leaks)

Source: Internet
Author: User

Here is an example:

Procedure Tmainform.button1click (Sender:tobject);
Var
Arr:array of array of string;
Begin
SetLength (arr,2,3);
Arr[1,2]:= ' This is a test ';
SetLength (arr,0,0);
SetLength (arr,4,5);
ShowMessage (arr[1,2]);

End

The way to declare a two-dimensional array is to use the array of array of type which is very close to the way people think. Similarly, the Declaration method for a three-dimensional array is an array of array of array of type

Resizing a two-dimensional array still uses the SetLength function, the first parameter is the array name, the second parameter is the size closest to the dimension of the array name, the third parameter is the size of the dimension away from the array name, and so on.

There is one particular point in the above example. is to declare a larger range of two-dimensional arrays, if the array is redistributed size, and the new size is smaller than the original array, then the large two-dimensional array and the small two-dimensional array of the difference between the parts will be cleared (supplementary sentence, if the type of the array is not the basic type, For example, if you declare the Can:array of Tcanvas array type Tcanvas, then the destructor is not automatically called in this case, so it is easy to create a memory leak, so be sure to pay attention to this use when programming. Even if you immediately resize the array back even larger, you can't get the data back. This is not the same as the Tstringgrid control provided in Delphi. After adjusting the number of Tstringgrid, the original data will not be lost unless the TStringGrid.Rows.Clear is used;

In addition, Delphi also allows the setting of non-matrix dynamic arrays. Here is an example

Procedure Tmainform.button1click (Sender:tobject);
Var
Arr:array of array of string;
Begin
SetLength (arr,10);
SetLength (arr[2],5);
Arr[2,4]:= ' This is a test ';
ShowMessage (arr[2,4]);
ShowMessage (arr[3,1]);
End

This declares a two-dimensional array of arr, first set to the first dimension of the array (by custom, called "column"), with a size of 10. The number of rows in the third column of the array (the subscript is 2 but actually the third column) is then set individually, with a size of 5. This makes up a matrix like this.

n n X n n n n n n. N n

n n X n n n n n n. N n

n n X n n n n n n. N n

n n X n n n n n n. N n

n n * n n n n n n. N n

("X" stands for "N" means unavailable "*" stands for arr[2,4] element)

The arr[2,4] is then assigned the "This is a test". First asked to display the value of arr[2,4], OK, absolutely no problem, but to the arr[3,1] when the error, the reason is that the two-dimensional array is not a matrix form, in addition to the third column, other places do not exist, although it has been declared.

http://blog.csdn.net/chaijunkun/article/details/5373597

About declaring and resizing two-dimensional arrays in Delphi (for non-basic types of data, beware of memory leaks)

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.