Docsity

CS 213 Class Notes: Arrays and Pointers, Slides of Introduction to Computers

Partial preview of the text

Download CS 213 Class Notes: Arrays and Pointers and more Slides Introduction to Computers in PDF only on Docsity!

Structured Data

Sept. 17, 1998

Topics

  • Arrays
    • Single
    • Nested
  • Pointers
    • Multilevel Arrays
  • Optimized Array Code

class08.ppt

“The course that gives CMU its Zip!”

Basic Data Types

Integral

  • Stored & operated on in general registers
  • Signed vs. unsigned depends on instructions used Alpha Bytes C byte 1 [ unsigned ] char word 2 [ unsigned ] short long word 4 [ unsigned ] int quad word 8 [ unsigned ] long int, pointers

Floating Point

  • Stored & operated on in floating point registers
  • Special instructions for four different formats (only 2 we care about) Alpha Bytes C S_floating 4 float T_floating 8 double

Array Access

Basic Principle

T A[ L ];

  • Array of data type T and length L
  • Identifier A can be used as a pointer to starting element of the array

Reference Type Value

val[4] int 3 val int * x val+1 int * x + 4 &val[2] int * x + 8 val[5] int ?? *(val+1) int 5 val + i int * x + 4 i

int val[5]; 1 5 2 1 3

x (^) x + 4 x + 8 x + 12 x + 16 x + 20

Array Example

Notes

  • Declaration “ zip_dig cmu ” equivalent to “ int cmu[5] ”
  • Example arrays were allocated in successive 20 byte blocks
    • Not guaranteed to happen in general

typedef int zip_dig[5];

zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 };

zip_dig cmu; (^1 5 2 1 )

16 20 24 28 32 36 zip_dig mit; (^0 2 1 3 )

36 40 44 48 52 56 zip_dig ucb; (^9 4 7 2 )

56 60 64 68 72 76

Referencing Examples

Code Does Not Do Any Bounds Checking!

Reference Address Value Guaranteed?

mit[3] 36 + 4* 3 = 48 3 Yes mit[5] 36 + 4* 5 = 56 9 No mit[-1] 36 + 4-1 = 32 3 No cmu[15] 16 + 415 = 76 ?? No

  • Out of range behavior implementation-dependent
    • No guranteed relative allocation of different arrays

zip_dig cmu; (^1 5 2 1 )

16 20 24 28 32 36 zip_dig mit; (^0 2 1 3 )

36 40 44 48 52 56 zip_dig ucb; (^9 4 7 2 )

56 60 64 68 72 76

int zd2int(zip_dig z) { int i; int zi = 0; for (i = 0; i < 5; i++) { zi = 10 * zi + z[i]; } return zi; }

bis $31,$31,$0 # zi = 0 bis $31,$31,$3 # i = 0 $61: # loop s4addl $0,$0,$2 # 5zi addq $2,$2,$2 # 10zi s4addq $3,$16,$1 # &z[i] ldl $1,0($1) # z[i] addl $2,$1,$0 # z[i]=10*zi+z[i] addl $3,1,$3 # i++ cmple $3,4,$1 # if i<= bne $1,$61 # goto loop ret $31,($26),

Array Loop Example

Registers

$16 z $0 zi $3 i

Computations

  • 10 * x implemented as (4+1) * x + (4+1) * x - Avoids use of slow multiply instruction
  • Digit access as 4*$3 + $

Nested Array Allocation

Declaration

T A[ R ][ C ];

  • Array of data type T
  • R rows
  • C columns
  • Type T element requires K bytes

Array Size

  • R * C * K bytes

Arrangement

  • Row-Major Ordering

a[0][0] a[0][C-1]

a[R-1][0]

  • • • a[R-1][C-1]

int A[R][C];

A [0] [0]

A

[0]

[C-1]

A

[1]

[0]

A

[1]

[C-1]

A

[R-1]

[0]

A

[R-1]

[C-1]

4RC Bytes

Nested Array Row Access

Row Vectors

  • A[i] is array of C elements
  • Each element of type T
  • Starting address A + i * C * K

A

[i] [0]

A

[i] [C-1]

A[i]

A

[R-1]

[0]

A

[R-1]

[C-1]

A[R-1]

A

A

[0]

[0]

A

[0]

[C-1]

A[0]

int A[R][C];

A+iC4 A+(R-1)C

Nested Array Element Access

Array Elements

  • A[i][j] is element of type T
  • Address A + ( i * C + j ) * K

A

[i] [j]

A

[i] [j]

A[i]

A

[R-1]

[0]

A

[R-1]

[C-1]

A[R-1]

A

A

[0]

[0]

A

[0]

[C-1]

A[0]

int A[R][C];

A+iC4 A+(R-1)C

A+(iC+j)

Nested Array Element Access Code

Array Elements

  • pgh[index][dig] is int
  • Address: pgh + 20index + 4dig

Code

  • Computes address
  • Loads and returns value

int get_pgh_digit (int index, int dig) { return pgh[index][dig]; }

get_pgh_digit..ng: s4addq $16,$16,$16 # 5 index lda $1,pgh # pgh s4addq $16,$1,$16 # pgh + 20index s4addq $17,$16,$17 # pgh+20index+4dig ldl $0,0($17) # pgh[index][dig] ret $31,($26),

Multi-Level Array Example

  • Variable univ denotes array of 3 elements
  • Each element is a pointer - 8 bytes
  • Each pointer points to array of int ’s

zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 };

#define UCOUNT 3 int *univ[UCOUNT] = {mit, cmu, ucb};

univ

cmu 1 5 2 1 3

16 20 24 28 32 36 mit 0 2 1 3 9

ucb^36 40 44 48 52 9 4 7 2 0

56 60 64 68 72 76

Referencing “Row” in Multi-Level Array

Row Vector

  • univ[index] is pointer to array of int ’s
  • Starting address Mem[univ+8*index]

Code

  • Computes address within univ
  • Loads and returns pointer

int* get_univ_zip(int index) { return univ[index]; }

get_univ_zip..ng: lda $1,univ # univ s8addq $16,$1,$16 # univ+8*index ldq $0,0($16) # univ[index] ret $31,($26),

Strange Referencing Examples

Reference Address Value Guaranteed?

univ[2][3] 56+43 = 68 2 Yes univ[1][5] 16+45 = 36 0 No univ[2][-1] 56+4-1 = 52 9 No univ[3][-1] ?? ?? No univ[1][12] 16+412 = 64 7 No

  • Code does not do any bounds checking
  • Ordering of elements in different arrays not guaranteed

univ

cmu 1 5 2 1 3

16 20 24 28 32 36 mit 0 2 1 3 9

ucb^36 40 44 48 52 9 4 7 2 0

56 60 64 68 72 76

Using Nested Arrays

Strengths

  • C compiler handles doubly subscripted arrays
  • Generates very efficient code - Avoids multiply in index computation

Limitation

  • Only works if have fixed array size

#define N 16 typedef int fix_matrix[N][N];

/* Compute element i,k of fixed matrix product / int fix_prod_ele (fix_matrix a, fix_matrix b, long int i, long int k) { long int j; int result = 0; for (j = 0; j < N; j++) result += a[i][j]b[j][k]; return result; }

A

(i,*) B

(*,k)

Column-wise

Row-wise