0% found this document useful (0 votes)
60 views

09 Pointers Arrays

Arrays in C are contiguous blocks of memory that store elements of the same data type. Pointers can be used to access array elements using pointer arithmetic. Multi-dimensional arrays lay out elements row-by-row, so an element at row i and column j would be located at base_address + i * number_of_columns_per_row * element_size + j * element_size. Looping through arrays in an optimized manner, such as using pointer increments instead of index operations, can improve performance.

Uploaded by

sibti007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

09 Pointers Arrays

Arrays in C are contiguous blocks of memory that store elements of the same data type. Pointers can be used to access array elements using pointer arithmetic. Multi-dimensional arrays lay out elements row-by-row, so an element at row i and column j would be located at base_address + i * number_of_columns_per_row * element_size + j * element_size. Looping through arrays in an optimized manner, such as using pointer increments instead of index operations, can improve performance.

Uploaded by

sibti007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Pointers and

Arrays

1
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

2
The address-of operator: &

The dereference operator: *

Arrays and pointers closely related.

3
Pointers and arrays review

4
Pointers and arrays review

1
4
8

5
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 x + 8 x + 16 x + 24

char *p[3];
x x + 8 x + 16 x + 24

6
Pointer arithmetic with arrays

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

7
animation
Array Accessing Example
int myNums[5];
1 5 2 1 3

16 20 24 28 32 36
%rdi %rsi 4

int get_num (int z[], int num {


Base + Index size
return z[num]; %rdi + %rsi 4
}
(%rdi,%rsi,4)
Assembly Code uses scaled, indexed mode
# On entry:
# %rdi = z
# %rsi = num
movl (%rdi,%rsi,4),%eax

8
Practice Problem
short S [7];
short *T [3]
short * * U [6];

9
Practice Problem
short S [7];
short *T [3]
short * * U [6];

2 14 x+2i
8 24 x+8i
8 48 x+8i

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

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

11
animation
Arrays as function arguments

int foo(int* f) { }

Pointer to the first element of array!


Arrays are passed by reference
12
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;
}
13
argv

14
animation
Problem

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

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

char ** pLines
char * a
char * a
char a
char a
char a

15
Array access in C

int a[10];
a a+4

a:
a[0] a[4] a[9]

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

16
animation
Array access examples
int val[5];
val: 1 5 2 1 3

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

int 3
int * x
int * x+12
int * x+8
int ?
int 5
int * x+4i

17
Arrays in Assembly

18
1 5 2 1 3
Example val[0] val[1] val[2] val[3] val[4]

int decimal5 (int *x) { int decimal5_opt (int *x) {


int i; int val = 0;
int val = 0; int *xend = x + 4;

for (i = 0; i < 5; i++) do {


val = (10 * val) + x[i]; val = (10 * val) + *x;
x++;
return val; } while (x <= xend);
}
return val;
}
19
decimal5_opt:
leaq 16(%rdi),%rdx
movl $0, %eax
.L45:
leal (%rax,%rax,4),%eax
addl %eax,%eax
addl (%rdi),%eax int decimal5_opt (int *x) {
addq $4,%rdi int val = 0;
cmpq %rdx,%rdi int *xend = x + 4;
jbe .L45
rep ret do {
val = (10 * val) + *x;
x++;
} while (x <= xend);

return val;
}
20
Multi-Dimensional Arrays C columns

R rows
21
Multi-Dimensional Array Access

C*K

A + 2*C*K + 5*K

22
Multi-Dimensional Array Access

row 0 row i row R-1

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

23
Multi-Dimensional Array Access

row 0 row i row R-1

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

24
Multi-Dimensional Array Access

row 0 row i row R-1

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

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

25
Multi-Dimensional Array Access

row 0 row i row R-1

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

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

26
Multi-Dimensional Array Access

row 0 row i row R-1

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

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

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

27
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]

28
Improving Code Efficiency

A B 29
Dot Product
for i,k
C[i,k] = 0;
for all j
C[i,k] += A[i,j] B[j,k]

A B
j k k

i j i

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

int fix_prod_ele (fix_matrix A, fix_matrix B, int fix_prod_ele_opt(fix_matrix A,


int i, int k) fix_matrix B, int i, int k)
{ {
int j; int *Aptr = &A[i][0];
int result = 0; int *Bptr = &B[0][k];
int cnt = N - 1;
for (j = 0; j < N; j++) int result = 0;
result += A[i][j] * B[j][k];
do {
return result; result += (*Aptr) * (*Bptr);
} Aptr += 1;
Bptr += N;
cnt--;
} while (cnt >= 0);

return result;
}

31
#define N 16
typedef int fix_matrix[N][N]; f.L50:
movslq %r8d, %rdx
int fix_prod_ele (fix_matrix A, fix_matrix B, int i, int k) movq %rdx, %r9
{
int j; salq $6, %r9
int result = 0; addq %rsi, %r9
movl (%r9,%rcx,4), %r9d
for (j = 0; j < N; j++) imull (%rdi,%rdx,4), %r9d
result += A[i][j] * B[j][k]; addl %r9d, %eax
return result; addl $1, %r8d
} cmpl $15, %r8d
jle .L50
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; .L52:
movl (%rcx), %esi
do { imull (%rdi), %esi
result += (*Aptr) * (*Bptr); addl %esi, %eax
Aptr += 1; addq $4, %rdi
Bptr += N; addq $64, %rcx
cnt--; subl $1, %edx
} while (cnt >= 0); jns .L52
return result;
}

32
Dynamically Allocated Arrays

33
Accessing Dynamic Arrays

34

You might also like