deconstructing_20arrays
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
deconstructing_20arrays [2018/03/31 13:19] – external edit 127.0.0.1 | deconstructing_20arrays [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 4: | Line 4: | ||
==== Reading the array pointer ==== | ==== Reading the array pointer ==== | ||
\\ The most important part of an array is the pointer to the array data block and reading this is the first step in deconstructing an array. But we also need an array to deal with and one such example is created below.\\ \\ | \\ The most important part of an array is the pointer to the array data block and reading this is the first step in deconstructing an array. But we also need an array to deal with and one such example is created below.\\ \\ | ||
+ | <code bb4w> | ||
DIM array(1, 2, 3, 4) | DIM array(1, 2, 3, 4) | ||
parr% = ^array(): | parr% = ^array(): | ||
+ | </ | ||
\\ Here we dimension an example array **array()** and set **parr%** to the pointer to this array.\\ \\ | \\ Here we dimension an example array **array()** and set **parr%** to the pointer to this array.\\ \\ | ||
==== Basic array information ==== | ==== Basic array information ==== | ||
\\ | \\ | ||
+ | <code bb4w> | ||
PRINT " | PRINT " | ||
PRINT " | PRINT " | ||
PRINT " | PRINT " | ||
PRINT " | PRINT " | ||
+ | </ | ||
\\ Here we display basic information about the array. The pointer to the parameter block could be expressed as **^array()**, | \\ Here we display basic information about the array. The pointer to the parameter block could be expressed as **^array()**, | ||
==== Reading the size of each dimension ==== | ==== Reading the size of each dimension ==== | ||
\\ To find the size of each dimension in the array and calculate the total number of elements in the array use the following code:\\ \\ | \\ To find the size of each dimension in the array and calculate the total number of elements in the array use the following code:\\ \\ | ||
+ | <code bb4w> | ||
tele% = 1:REM Total number of elements | tele% = 1:REM Total number of elements | ||
FOR i% = 0 TO ?!parr% - 1 | FOR i% = 0 TO ?!parr% - 1 | ||
Line 23: | Line 28: | ||
NEXT i% | NEXT i% | ||
PRINT "Total number of elements: "; | PRINT "Total number of elements: "; | ||
+ | </ | ||
\\ Here we iterate through the dimensions of the array and read the number of elements in each dimension (**nele%**) and display the result. The running total number of elements in the array is calculated and stored in **tele%**, this value is used to calculate the size of the array data block.\\ \\ | \\ Here we iterate through the dimensions of the array and read the number of elements in each dimension (**nele%**) and display the result. The running total number of elements in the array is calculated and stored in **tele%**, this value is used to calculate the size of the array data block.\\ \\ | ||
==== Determining the array type ==== | ==== Determining the array type ==== | ||
\\ Arrays can contain different types of variable and each variable type occupies a different number of bytes in memory. Knowing the type of an array is important when manipulating the data in the array or the array itself. The array variable name is precedes the array pointer in memory. This allows us to find the type of variable stored in the array. The variable type is stored three bytes before the array pointer.\\ \\ We can use this information to find the type of variable stored in the array:\\ \\ | \\ Arrays can contain different types of variable and each variable type occupies a different number of bytes in memory. Knowing the type of an array is important when manipulating the data in the array or the array itself. The array variable name is precedes the array pointer in memory. This allows us to find the type of variable stored in the array. The variable type is stored three bytes before the array pointer.\\ \\ We can use this information to find the type of variable stored in the array:\\ \\ | ||
+ | <code bb4w> | ||
PRINT "Array type: "; | PRINT "Array type: "; | ||
CASE parr%?-3 OF | CASE parr%?-3 OF | ||
Line 35: | Line 42: | ||
ENDCASE | ENDCASE | ||
PRINT "Size of each element: "; | PRINT "Size of each element: "; | ||
+ | </ | ||
\\ Here **esize%** is set to the size in bytes of the variable type stored in the array. One variable type, the 40-bit real, does not have a type suffix so it is possible to assume that any variable name that does not end in **%**, **#**, **$** or **&** is a 40-bit real. When BBC BASIC is in //float 64// mode all new real variables have the **#** suffix silently added to the end of the name. This is essential for BBC BASIC to tell the difference between the two types of real variable.\\ \\ **Note:** We are ignoring structures and arrays here as these variable types cannot be contained within an array. Structure variable names have a ' | \\ Here **esize%** is set to the size in bytes of the variable type stored in the array. One variable type, the 40-bit real, does not have a type suffix so it is possible to assume that any variable name that does not end in **%**, **#**, **$** or **&** is a 40-bit real. When BBC BASIC is in //float 64// mode all new real variables have the **#** suffix silently added to the end of the name. This is essential for BBC BASIC to tell the difference between the two types of real variable.\\ \\ **Note:** We are ignoring structures and arrays here as these variable types cannot be contained within an array. Structure variable names have a ' | ||
==== Calculating the total size of the array ==== | ==== Calculating the total size of the array ==== | ||
\\ The total size of the array data area equals the total number of elements (**tele%**) multiplied by the size of each element (**esize%**). The total size of the array block equals the size of the data area plus one byte for the number of dimensions and four extra bytes for the description of each dimension.\\ \\ We can now use all the information collected so far to determine the total size of the array in memory:\\ \\ | \\ The total size of the array data area equals the total number of elements (**tele%**) multiplied by the size of each element (**esize%**). The total size of the array block equals the size of the data area plus one byte for the number of dimensions and four extra bytes for the description of each dimension.\\ \\ We can now use all the information collected so far to determine the total size of the array in memory:\\ \\ | ||
+ | <code bb4w> | ||
PRINT "Total data area size: "; | PRINT "Total data area size: "; | ||
PRINT "Total array size: "; | PRINT "Total array size: "; | ||
+ | </ | ||
\\ | \\ | ||
==== How to verify the array pointer ==== | ==== How to verify the array pointer ==== | ||
\\ When writing code that manipulates arrays by reference it is a good idea to check that the array pointer **parr%** is actually a pointer to an array. Not doing so will lead to undefined behaviour and may crash //BBC BASIC// if your code is called with an invalid array pointer.\\ \\ To check if a pointer points to an array use the following code:\\ \\ | \\ When writing code that manipulates arrays by reference it is a good idea to check that the array pointer **parr%** is actually a pointer to an array. Not doing so will lead to undefined behaviour and may crash //BBC BASIC// if your code is called with an invalid array pointer.\\ \\ To check if a pointer points to an array use the following code:\\ \\ | ||
+ | <code bb4w> | ||
IF NOT (parr%?-1 = 0 AND parr%?-2 = 40) THEN | IF NOT (parr%?-1 = 0 AND parr%?-2 = 40) THEN | ||
REM parr% does not point to an array | REM parr% does not point to an array | ||
Line 48: | Line 59: | ||
REM parr% points to an array | REM parr% points to an array | ||
ENDIF | ENDIF | ||
+ | </ | ||
\\ Here we check for a variable descriptor that matches an array. If the array pointer **parr%** does not point to a valid array block the routine should not attempt to access or manipulate data in the invalid array. | \\ Here we check for a variable descriptor that matches an array. If the array pointer **parr%** does not point to a valid array block the routine should not attempt to access or manipulate data in the invalid array. |
deconstructing_20arrays.1522502354.txt.gz · Last modified: 2024/01/05 00:18 (external edit)