Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare an array when I don't know the length until run time?

I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time.

Context is filling an array with a linear transformation of the bytes in a file. I don't know how big the file is until someone wants to open it and the files can be of any size.

like image 840
Arthur Avatar asked Jan 11 '09 17:01

Arthur


People also ask

How do you declare an array of unknown lengths?

int[] list = new int[5];

Can we declare an array without defining a size or adding any elements at the time of declaration?

You can declare an array without a size specifier for the leftmost dimension in multiples cases: as a global variable with extern class storage (the array is defined elsewhere), as a function parameter: int main(int argc, char *argv[]) . In this case the size specified for the leftmost dimension is ignored anyway.

How do you declare the length of an array?

var-name = new type [size]; Here, type specifies the type of data being allocated, size determines the number of elements in the array, and var-name is the name of the array variable that is linked to the array. To use new to allocate an array, you must specify the type and number of elements to allocate.

Can we declare size of array at run time?

No. In an array declaration, the size must be known at compile time. You can�t specify a size that�s known only at runtime.


1 Answers

As of Delphi 4, Delphi supports dynamic arrays. You can modify their sizes at run time and they will retain the data you stored in other elements at the old size. They can hold elements of any homogeneous type, including records and other arrays. You can declare a dynamic array the same as you declare normal, "static" arrays, but simply omit the array bounds:

var   ArthurArray: array of TForm; 

Although static arrays allow you to specify both the lower and upper bound, the low index of a dynamic array is always zero. The high index is given by the High function, which always returns one less than the length of the array. For any dynamic array x, High(x) = Length(x)-1.

A global variable can be accessed by any code, including local procedures.

A global variable of dynamic-array type will be initialized to be an empty array. Its length will be zero and High called on that array will be -1. Low on that array will still return zero.

At any time, you may resize a dynamic array. Use the SetLength function, just as you can do with strings:

var   NumElements: Integer; begin   NumElements := GetNumberOfArthurForms();   SetLength(ArthurArray, NumElements); end; 

If you have a multidimensional array, you can set their lengths in a loop:

var   matrix: array of array of Double;   i: Integer; begin   SetLength(matrix, height);   for i := 0 to height - 1 do     SetLength(matrix[i], width); end; 

There's a shortcut for that to set the lengths of all the inner arrays at once:

begin   SetLength(matrix, height, width); end; 

Like I mentioned, dynamic arrays keep their old values when you resize them:

var   data: array of string; begin   SetLength(data, 2);   data[1] := 'foo';   SetLength(data, 20);   Assert(data[1] = 'foo'); end; 

But if you shorten the array, any elements that resided beyond the new last element are gone forever:

begin   SetLength(data, 20);   data[15] := 'foo';   SetLength(data, 2);   // data[15] does not exist anymore.   SetLength(data, 16);   writeln(data[15); // Should print an *empty* line. end; 

My demonstrations above used strings. Strings are special in Delphi; they're managed by the compiler through reference counts. Because of that, new dynamic-array elements of type string are initialized to be empty. But if I had used integers instead, there would be no guarantee of the values of new elements. They might be zero, but they might be anything else, too, just like the initial values of standalone local variables.

The Delphi 7 help files are very good, I'm told. Please read more about dynamic arrays there. You can find demonstrations of their use throughout the VCL and RTL source code provided in your Delphi installation, as well as in nearly any Delphi code example produced in the last 10 years.

like image 61
Rob Kennedy Avatar answered Sep 23 '22 16:09

Rob Kennedy