pointers and arrays - computer action team13 arrays of pointers #include #include char...

34
Pointers and Arrays 1

Upload: others

Post on 09-Aug-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

Pointers and Arrays

1

Page 2: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

2

Every pointer has a type

Every pointer has a value The address of an object (of a particular type) All pointers are 8 bytes for x86-64 8 bytes = 64 bits

Page 3: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

3

The “address-of” operator: &

The “dereference” operator: *

Arrays and pointers closely related.

Page 4: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

4

Pointers and arrays review

Page 5: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

5

Pointers and arrays review

1 4 8

Page 6: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

6

Array allocation Type ArrayName [Length];

Contiguously allocated region of Length × sizeof(Type) bytes

char string[12];

x   x  +  12  

int val[5];

x   x  +  4   x  +  8   x  +  12   x  +  16   x  +  20  

double a[3];

x  +  24  x   x  +  8   x  +  16  

char *p[3];

x   x  +  8   x  +  16   x  +  24  

Page 7: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

7

Pointer arithmetic with arrays

char A[12]; int B[8]; double C[6]; int *D[5]

Page 8: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

8

Array  Accessing  Example  

int get_num (int z[], int num { return z[num]; }

# On entry: # %rdi = z # %rsi = num movl (%rdi,%rsi,4),%eax

int myNums[5];

1   5   2   1   3  

16   20   24   28   32   36  

Assembly Code uses scaled, indexed mode

Base + Index × size %rdi + %rsi × 4

%rdi %rsi 4

(%rdi,%rsi,4)

animation

Page 9: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

9

Practice Problem

short S [7]; short *T [3] short * * U [6];

Page 10: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

10

Practice Problem

short S [7]; short *T [3] short * * U [6];

2 14 x+2i

8 24 x+8i

8 48 x+8i

Page 11: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

11

zincr: # %rdi = z movl $0, %eax # i = 0 jmp .L3 # goto test .L4: # loop: addl $1, (%rdi,%rax,4) # z[i]++ addq $1, %rax # i++ .L3: # test: cmpq $4, %rax # i:4 jbe .L4 # if <=, goto loop rep; ret

Array  Loop  Example  void zincr(int z[5]) { size_t i; for (i = 0; i < 5; i++) z[i]++; }

Page 12: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

12

Arrays as function arguments

int foo(int* f) { … }

Pointer to the first element of array! Arrays are passed by reference

animation

Page 13: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

13

Arrays of pointers

#include <stdlib.h> #include <stdio.h>

char *monthName(int n) { static char *name[] = { "Illegal month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return ( n < 1 || n > 12 ) ? name[0] : name[n]; }

int main(int argc, char *argv[]) { if (2 != argc) { fprintf(stderr, "Usage: %s <int>\n", argv[0]); return 0;` } printf("%s\n", monthName(atoi(argv[1]))); return 0; }

Page 14: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

14

argv

Page 15: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

15

Problem

char ** pLines

char * a char * a

char ‘a’

char ‘a’ char ‘a’

animation

char *pLines[3]; char *a="abc"; char *d="def"; char *g="ghi";

pLines[0]=a; pLines[1]=d; pLines[2]=g;

Page 16: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

16

Array access in C

(a+N) == &a[N] *(a+N) == a[N]

int a[10];

a: a[0] a[9]

a+4

a[4]

a

Page 17: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

17

Array access examples

1 5 2 1 3

int val[5];

x x + 4 x + 8 x + 12 x + 16 x + 20

val:

int 3

int * x+4i

int * x

int * x+12

int * x+8

int ? int 5

animation

Page 18: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

18

Arrays in Assembly

Page 19: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

19

Example

int decimal5 (int *x) { int i; int val = 0;

for (i = 0; i < 5; i++) val = (10 * val) + x[i];

return val; }

int decimal5_opt (int *x) { int val = 0; int *xend = x + 4;

do { val = (10 * val) + *x; x++; } while (x <= xend);

return val; }

1 5 2 1 3

val[0] val[1] val[2] val[3] val[4]

Page 20: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

20

decimal5_opt: leaq 16(%rdi),%rdx movl $0, %eax.L45: leal (%rax,%rax,4),%eax addl %eax,%eax addl (%rdi),%eax addq $4,%rdi cmpq %rdx,%rdi jbe .L45 rep ret

int decimal5_opt (int *x) { int val = 0; int *xend = x + 4;

do { val = (10 * val) + *x; x++; } while (x <= xend);

return val; }

Page 21: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

21

Multi-Dimensional Arrays C columns

R ro

ws

Page 22: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

22

Multi-Dimensional Array Access

C * K

A + 2*C*K + 5*K

Page 23: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

23

Multi-Dimensional Array Access

A[0][0]

row i row R-1

A

row 0

… A[0][C-1] A[i][j] A[R-1][C-1] A[R-1][C-1]… … … ……

Page 24: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

24

Multi-Dimensional Array Access

A[0][0]

row i row R-1

A

row 0

… A[0][C-1] A[i][j] A[R-1][C-1] A[R-1][C-1]… … … ……

Page 25: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

25

Multi-Dimensional Array Access

A[0][0]

row i row R-1

A

row 0

A+(i*C*4) A+((R-1)*C*4)

… A[0][C-1] A[i][j] A[R-1][C-1] A[R-1][C-1]… … … ……

Page 26: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

26

Multi-Dimensional Array Access

A[0][0]

row i row R-1

A

row 0

A+(i*C*4) A+((R-1)*C*4)

… A[0][C-1] A[i][j] A[R-1][C-1] A[R-1][C-1]… … … ……

Page 27: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

27

Multi-Dimensional Array Access

A[0][0]

row i row R-1

A

row 0

A+(i*C*4) A+((R-1)*C*4)

A+(i*C*4)+(j*4)

… A[0][C-1] A[i][j] A[R-1][C-1] A[R-1][C-1]… … … ……

Page 28: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

28

Watch Out For Indexing Errors

A + (C*i + j) * K = A + (13*3 + 26) * 4 = A + (13*5 + 0) * 4 Same as A[5][0]

Page 29: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

29

Improving Code Efficiency

A B

i

k

Page 30: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

30

Dot Product for i,k…

C[i,k] = 0;

for all j…

C[i,k] += A[i,j] × B[j,k]

A B

i

k j

j i

k

Page 31: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

31

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

int fix_prod_ele (fix_matrix A, fix_matrix B, int i, int k) { int j; int result = 0;

for (j = 0; j < N; j++) result += A[i][j] * B[j][k];

return result; }

int fix_prod_ele_opt(fix_matrix A, fix_matrix B, int i, int k)

{ int *Aptr = &A[i][0]; int *Bptr = &B[0][k]; int cnt = N - 1; int result = 0;

do { result += (*Aptr) * (*Bptr); Aptr += 1; Bptr += N; cnt--; } while (cnt >= 0);

return result; }

Page 32: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

32

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

int fix_prod_ele (fix_matrix A, fix_matrix B, int i, int k) { int j; int result = 0;

for (j = 0; j < N; j++) result += A[i][j] * B[j][k];

return result; }

int fix_prod_ele_opt(fix_matrix A, fix_matrix B, int i, int k) { int *Aptr = &A[i][0]; int *Bptr = &B[0][k]; int cnt = N - 1; int result = 0;

do { result += (*Aptr) * (*Bptr); Aptr += 1; Bptr += N; cnt--; } while (cnt >= 0);

return result; }

.L52: movl (%rcx), %esi imull (%rdi), %esi addl %esi, %eax addq $4, %rdi addq $64, %rcx subl $1, %edx jns .L52

f.L50: movslq %r8d, %rdx movq %rdx, %r9 salq $6, %r9 addq %rsi, %r9 movl (%r9,%rcx,4), %r9d imull (%rdi,%rdx,4), %r9d addl %r9d, %eax addl $1, %r8d cmpl $15, %r8d jle .L50

Page 33: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

33

Dynamically Allocated Arrays

Page 34: Pointers and Arrays - Computer Action Team13 Arrays of pointers #include  #include  char *monthName(int n) { static char *name[] = { "Illegal month",

34

Accessing Dynamic Arrays