40 likes | 119 Vues
Introduction to compilers Lecture 24 (Prepared By : HAYA SAMMA’ANEH) Array . 1- Ordered Sequence of objects (of same type). 2- Composite object : - Built from separate objects that can be individually accessed. 3- Declaration: - a : array [1 . . 10] of integer .
E N D
Introduction to compilersLecture 24 (Prepared By : HAYA SAMMA’ANEH) Array 1- Ordered Sequence of objects (of same type). 2- Composite object : - Built from separate objects that can be individually accessed. 3- Declaration: - a : array [1 . . 10] of integer . 4- Array reference : - a [5] . 5- Array slice : - a [1 : 5: 2 ] ---- a [1] , a [3] , a [5] .
Layout a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] ………. 1- Elements along a dimension stored in order . 2- Multidimensional arrays stored by row or column - a [1 .. 2] [ 1 .. 3] . - Row major . a[1,1] a[1,2] a[1,3] a[2,1] a[2,2] a[2,3] a[1,1] a[1,2] a[1,3] a[2,1] a[2,2] a[2,3] a[3,1] a[3,2] a[3,3] Row 1 Row 2 - Column major a[1,1] a[2,1] a[3,1] a[1,2] a[2,2] a[3,2] Column 1 Column 2
Accessing an Element 1- Address of a [i] - ( i - lower_ bound )* w + base - if lower_ bound=0 i * w + base 2- Address of a [i , j] - Row major ( i - low1 ) * w1 +( j - low2 )* w2+ base. . w1 is width of row , w2 is width of element . 3- Multiplication is big cost.
C Pointers and Arrays 1- C pointers can point to array elements . 2- Programmers perform pointer arithmetic to access elements . 3- in simple cases , eliminate multiplications by repeated addition. - for i := 1 to 10 do f (a [i]) - for i := 1 to 10 do f ( i * 4 + & a ) - for ( p= & a , i = 1 ; i <= 10 ; i++ ; p++ ) f ( p ) 4- But a compiler can perform optimization also - for ( p = & a ; p<= & a + 40 ; p++ ) f ( p )