4.26 Allocating Storage for Multidimensional Arrays


4.26 Allocating Storage for Multidimensional Arrays

If you have an m x n array, it will have m * n elements and require m*n*Element_Size bytes of storage. To allocate storage for an array you must reserve this memory. As usual, there are several different ways of accomplishing this task. Fortunately, HLA's array declaration syntax is very similar to high level language array declaration syntax, so C/C++, BASIC, and Pascal programmers will feel right at home. To declare a multidimensional array in HLA, you use a declaration like the following:

      ArrayName: elementType [ comma_separated_list_of_dimension_bounds ]; 

For example, here is a declaration for a 4x4 array of characters:

 GameGrid: char[ 4, 4 ]; 

Here is another example that shows how to declare a three-dimensional array of strings:

 NameItems: string[ 2, 3, 3 ]; 

Remember, string objects are really pointers, so this array declaration reserves storage for 18 double word pointers (2*3*3=18).

As was the case with single dimension arrays, you may initialize every element of the array to a specific value by following the declaration with the assignment operator and an array constant. Array constants ignore dimension information; all that matters is that the number of elements in the array constant correspond to the number of elements in the actual array. The following example shows the GameGrid declaration with an initializer:

      GameGrid: char[ 4, 4 ] :=           [                'a', 'b', 'c', 'd',                'e', 'f', 'g', 'h',                'i', 'j', 'k', 'l',                'm', 'n', 'o', 'p'           ]; 

Note that HLA ignores the indentation and extra whitespace characters (e.g., newlines) appearing in this declaration. It was laid out to enhance readability (which is always a good idea). HLA does not interpret the four separate lines as representing rows of data in the array. Humans do, which is why it's good to lay out the initial data in this manner, but HLA completely ignores the physical layout of the declaration. All that matters is that there are 16 (4*4) characters in the array constant. You'll probably agree that this is much easier to read than

      GameGrid: char[ 4,4 ] :=           [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',             'n', 'o', 'p' ]; 

Of course, if you have a large array, an array with really large rows, or an array with many dimensions, there is little hope for winding up with something readable. That's when comments that carefully explain everything come in handy.

As with single dimension arrays, you can use the dup operator to initialize each element of a really large array with the same value. The following example initializes a 256x64 array of bytes so that each byte contains the value $FF:

           StateValue: byte[ 256, 64 ] := 256*64 dup [$ff]; 

Note the use of a constant expression to compute the number of array elements rather than simply using the constant 16,384 (256*64). The use of the constant expression more clearly suggests that this code is initializing each element of a 256x64 element array than does the simple literal constant 16,384.

Another HLA trick you can use to improve the readability of your programs is to use nested array constants. The following is an example of an HLA nested array constant:

 [ [0, 1, 2], [3, 4], [10, 11, 12, 13] ]

Whenever HLA encounters an array constant nested inside another array constant, it simply removes the brackets surrounding the nested array constant and treats the whole constant as a single array constant. For example, HLA converts the preceding nested array constant to the following:

 [ 0, 1, 2, 3, 4, 10, 11, 12, 13 ] 

You can take advantage of this fact to help make your programs a little more readable. For multidimensional array constants you can enclose each row of the constant in square brackets to denote that the data in each row is grouped and separate from the other rows. As an example, consider the following declaration for the GameGrid array that is identical (as far as HLA is concerned) to the previous declaration:

      GameGrid: char[ 4, 4 ] :=           [                [ 'a', 'b', 'c', 'd' ],                [ 'e', 'f', 'g', 'h' ],                [ 'i', 'j', 'k', 'l' ],                [ 'm', 'n', 'o', 'p' ]           ]; 

This declaration makes it clearer that the array constant is a 4x4 array rather than just a 16-element one-dimensional array whose elements wouldn't fit all on one line of source code. Little aesthetic improvements like this are what separate mediocre programmers from good programmers.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net