Pointers and Arrays - Computer Action...
Transcript of Pointers and Arrays - Computer Action...
![Page 1: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/1.jpg)
Pointers and Arrays
1
![Page 2: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/2.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/3.jpg)
3
The “address-of” operator: &
The “dereference” operator: *
Arrays and pointers closely related.
![Page 4: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/4.jpg)
4
Pointers and arrays review
![Page 5: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/5.jpg)
5
Pointers and arrays review
1 4 8
![Page 6: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/6.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/7.jpg)
7
Pointer arithmetic with arrays
char A[12]; int B[8]; double C[6]; int *D[5]
![Page 8: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/8.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/9.jpg)
9
Practice Problem
short S [7]; short *T [3] short * * U [6];
![Page 10: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/10.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/11.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/12.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/13.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/14.jpg)
14
argv
![Page 15: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/15.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/16.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/17.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/18.jpg)
18
Arrays in Assembly
![Page 19: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/19.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/20.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/21.jpg)
21
Multi-Dimensional Arrays C columns
R ro
ws
![Page 22: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/22.jpg)
22
Multi-Dimensional Array Access
C * K
A + 2*C*K + 5*K
![Page 23: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/23.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/24.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/25.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/26.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/27.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/28.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/29.jpg)
29
Improving Code Efficiency
A B
i
k
![Page 30: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/30.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/31.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/32.jpg)
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 Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/33.jpg)
33
Dynamically Allocated Arrays
![Page 34: Pointers and Arrays - Computer Action Teamweb.cecs.pdx.edu/~harry/cs201/slides/09PointersArrays.pdf · 3 The “address-of” operator: & The “dereference” operator: * Arrays](https://reader034.fdocuments.net/reader034/viewer/2022043010/5fa0f3808ded1f06e628a876/html5/thumbnails/34.jpg)
34
Accessing Dynamic Arrays