05 August 2010

Initializing an Arrray

An array is a named collection of values of the same type. In BASIC an array is always dynamic, the memory necessary to store the array elements is allocated at runtime. You cannot set the array elements to a constant value before hand. Initializing array elements is a runtime operation performed after the array is created, unfortunately. Let me illustrate.
In C/C++ you can assign initial values to an array by enclosing values within braces {}. For instance

int scores[5] = {1, 2, 3, 4, 5};

In contrast with BASIC, the array is static and not created dynamically at runtime. The C/C++ compiler reserves 5 consecutive integers (each 4-bytes, thus 20 bytes) in the .rdata section of executable and associates the address with the variable name scores. When the executable is loaded the 5 integer values are loaded as well and scores points to the first integer (value=1). Because the array is of type int (32-bits integer) the C/C++ compiler converts the ASCII representation "1" found in the source code to the 32-bits number 1. The executable contains the following array data, assuming the scores is stored at 0x0498760:

variable address value
scores[0] 0498760 1
scores[1] 0498764 2
scores[2] 0498768 3
scores[3] 049876C 4
scores[4] 0498770 5

In C/C++ the initialization is performed at compile time. So, when the C/C++ program is started no time is spent on assigning values to the array elements.
This in contrast with (classic) BASIC. The only method of initializing an array is by explicitly assigning each value to the array elements. Often, the values are stored in a DATA statement and then assigned using READ in a For-Next loop. To accomplish the same in BASIC you will often see the following code.

Dim scores(4) As Int, i As Int
For i = 0 To 4
  Read scores(i)
Next
Data 1,2,3,4,5

At compile time, the compiler doesn't know the data type of the Data-items. This is determined by the datatype of the variable used in the READ command and known at runtime only. After compiling the values in the Data line are stored as ASCII strings. The Read command converts the ASCII "1" to a 32-bit integer (because scores() is declared as an Int). After READ completes the conversion of the first value, it increments the data-pointer _Data. The next Read statement converts the next string ("2") using an ASCII-to-Int function. (The runtime function used is READINT() ). A rather clumsy, inelegant, and (run)time consuming process. Fortunately, GFA-BASIC 32 provides a faster way to initialize an array, thereby skipping the ASCII-TO-INT process for each element. BASIC doesn't allow array access through pointers, so the dynamic allocation remains.

The Array command
To initialize an array with literal constants GFA-BASIC 32 offers the Array command. The Array command takes a string with data in binary format to initialize the elements.

Array scores() =  Mki$(1, 2, 3, 4, 5)

The string is nothing more than a container for the data; a piece of memory containing the values in their binary form. And, most importantly, the string is created at compile-time. The string data is stored in their binary 32-bit format (Mki()) and takes 5 * 4 bytes = 20 bytes in your program's code. The storage is the same as with an initialized C/C++ array. The Array command uses the length of the string to calculate the number of elements to create a one-dimension array. It then copies the data to the array. This process is about 6-8 times faster than the For-Next loop method. 

No comments:

Post a Comment