Concepts of Programming Languages Dr. Mohamed Yehia Dahab
Array Types An array is an aggregate of homogeneous data elements in which an individual element is identified by its position in the aggregate, relative to the first element
Array Design Issues What types are legal for subscripts? Are subscripting expressions in element references range checked? When are subscript ranges bound? When does allocation take place? What is the maximum number of subscripts? Can array objects be initialized? Are any kind of slices allowed?
Array Indexing Indexing (or subscripting) is a mapping from indices to elements array_name (index_value_list) an element Index Syntax FORTRAN, PL/I, Ada and matlab use parentheses Ada explicitly uses parentheses to show uniformity between array references and function calls because both are mappings Most other languages use brackets
Arrays Index (Subscript) Types FORTRAN, C and Java: integer only Ada and Pascal: any ordinal type (https://ideone.com/JPfzA9) program ideone; type Color = (Red, Orange, Black, White, Yellow, Green, Blue); var A1 : array [-10 .. 10] of real; A2 : array['A'..'Z'] of integer; A3 : array[False .. True] of Color ; A4 : array[Red .. Yellow] of integer; begin A1[-5] := 3.1; A2['C'] :=17; A3 [True] := Red; A4 [Yellow] := 55; end.
Arrays Index (Subscript) Types (Cont’) C, C++, Perl, and Fortran do not specify range checking (https://ideone.com/pE7XX3) #include <stdio.h> int main(void) { int Array[5]; Array[10] = 55; printf("\n%d\n",Array[10]); return 0; }
Arrays Index (Subscript) Types (Cont’) Java, ML, matlab, and C# specify range checking % Matlab Code A=[55 7 8 9]; display (A(1)) % display 55 display (A(0)) % index must be a positive integer or logical display (A(4)) % display 9 display (A(5)) % Attempted to access A(5); index out of bounds because numel(A)=4 display (A(3>1)) % display 55 display (A(1>3)) % display []
Subscript Binding and Array Categories Static: subscript ranges are statically bound and storage allocation is static (before run-time) Advantage: efficiency (no dynamic allocation) Fixed stack-dynamic: subscript ranges are statically bound, but the allocation is done at declaration time Advantage: space efficiency
Subscript Binding and Array Categories (Cont’) Stack-dynamic: subscript ranges are dynamically bound and the storage allocation is dynamic (done at run-time) Advantage: flexibility (the size of an array need not be known until the array is to be used) Fixed heap-dynamic: similar to fixed stack- dynamic: storage binding is dynamic but fixed after allocation (i.e., binding is done when requested and storage is allocated from heap, not stack)
Subscript Binding and Array Categories (continued) Heap-dynamic: binding of subscript ranges and storage allocation is dynamic and can change any number of times Advantage: flexibility (arrays can grow or shrink during program execution)
Subscript Binding and Array Categories (Cont’) C and C++ arrays that include static modifier are static C and C++ arrays without static modifier are fixed stack-dynamic int Arr[20]; Ada arrays can be stack-dynamic C and C++ provide fixed heap-dynamic arrays (using malloc/free or new/delete) Perl and JavaScript support heap-dynamic arrays
C Language (Fixed Heap-dynamic) Using malloc() function Allocates requested size of bytes and returns a pointer first byte of allocated space <pointer variable>=(<cast-type>*)malloc(<byte-size>); Using free() function to dellocate the previously allocated space
Cuda C/C++ using cudaMalloc() (Fixed Heap-dynamic) A void ** is just a pointer to a pointer to memory with an unspecified type Host and device memory are separate entities Device pointers point to GPU memory Host pointers point to CPU memory int size = 100 * sizeof (int); int *d_a; cudaMalloc((void **)&d_a, size); … cudaFree(d_a);
C Language (Fixed Heap-dynamic) (Cont’) #include <stdio.h> #include <stdlib.h> int main(){ int *ptr; ptr=(int*)malloc(10*sizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error!"); exit(0); } //using ptr free(ptr); return 0; https://ideone.com/7ojfAs
Java ArrayList (Heap-dynamic) import java.util.*; import java.io.*; class TestArrayList { public static void main (String[] args){ ArrayList A = new ArrayList(); A.add(30); A.add(130); A.add("3dfdf"); System.out.println(A.size()); System.out.println(A.get(1)); //zero based System.out.println(A.get(2)); } https://ideone.com/y4ooYK Heterogeneous Arrays
Java ArrayList (Heap-dynamic) (Cont’) import java.util.*; import java.io.*; class TestGenericArrayList { public static void main (String[] args){ ArrayList <Integer> intList = new ArrayList<Integer>(); ArrayList <String> stringList = new ArrayList<String>(); intList.add(30); intList.add(10); stringList.add("hggfhfg"); stringList.add("dgdfgdfgrtre"); System.out.println(intList.size()); System.out.println(stringList.get(1)); } https://ideone.com/DuWSNK
Array in Python A better analogy would be to the ArrayList class Built-in functions and methods: append() remove() len() count(object) + for concatenation … NumPy is an open-source add-on modules to Python that provide common mathematical and numerical routines in pre-compiled, fast functions
Array in Python (Cont’) A.append(12) A.insert(0,13) print (A) A[1] = 10 A = A * 3 R = range(4) A = A + R print (13 in A) A.sort() print (A[2:5]) A.append("erere") [13, 12] [13, 10, 13, 10, 13, 10] [13, 10, 13, 10, 13, 10, 0, 1, 2, 3] True [0, 1, 2, 3, 10, 10, 10, 13, 13, 13] [2, 3, 10] https://ideone.com/zJD4xl Heterogeneous Arrays https://ideone.com/zJD4xl
Array Initialization Some language allow initialization at the time of storage allocation C, C++, Java, C# example int list [] = {4, 5, 7, 83} Character strings in C and C++ char name [] = “freddie”; Arrays of strings in C and C++ char *names [] = {“Bob”, “Jake”, “Joe”]; Java initialization of String objects String[] names = {“Bob”, “Jake”, “Joe”};
Arrays Operations APL provides the most powerful array processing operations for vectors and matrixes as well as unary operators (for example, to reverse column elements) Ada allows array assignment but also catenation Fortran provides elemental operations because they are between pairs of array elements For example, + operator between two arrays results in an array of the sums of the element pairs of the two arrays
Rectangular and Jagged Arrays A rectangular array is a multi-dimensioned array in which all of the rows have the same number of elements and all columns have the same number of elements A jagged matrix has rows with varying number of elements Possible when multi-dimensioned arrays actually appear as arrays of arrays
Java is using Jagged arrays Java Example int array[][] = new int[3][]; array[0] = new int[3]; array[1] = new int[2]; array[2] = new int[5]; Java is using Jagged arrays What about the following programming language: Matlab Pascal C python
Slices A slice is some substructure of an array; nothing more than a referencing mechanism Slices are only useful in languages that have array operations
Slice Examples Fortran 95 Python Integer, Dimension (10) :: Vector Integer, Dimension (3, 3) :: Mat Integer, Dimension (3, 3, 4) :: Cube Vector (3:6) is a four element array Python A[2:5] is a three element array
Slices Examples in Fortran 95