41
F28PL1 Programming Languages Lecture 9 Programming in C - 4

F28PL1 Programming Languages Lecture 9 Programming in C - 4

Embed Size (px)

Citation preview

Page 1: F28PL1 Programming Languages Lecture 9 Programming in C - 4

F28PL1 Programming Languages

Lecture 9 Programming in C - 4

Page 2: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Arrays and typed pointers

type name [int];• allocates space for int elements of type

type * name;• allocates space for pointer to type• does not allocate space for elements

Page 3: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Array assignment

• cannot assign:– array to array– type pointer to array

• must copy element by element• cannot assign array to type pointer• must:– allocate space to type pointer– copy element by element

• can assign type pointer to type pointer

Page 4: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Dynamic Space Allocation

char * malloc(int)• allocates int bytes on heap• returns address of 1st byt • like new in Java• in stdlib

Page 5: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Dynamic Space Allocation

• use cast to coerce pointer to sequence of bytes to pointer to required type

• typical malloc use to allocate pointer to number of type:(type *)malloc(number*sizeof(type))

• e.g. malloc(24) pointer to 24 bytes

(int *)malloc(6*sizeof(int)) pointer to 6 ints == 24 bytes

Page 6: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Dynamic Space Allocation

free(void *)• returns space to heap• space must have been allocated by malloc

• NB does not recursively return space– must climb linked data structure freeing space

Page 7: F28PL1 Programming Languages Lecture 9 Programming in C - 4

String Dynamic Space Allocation

char * name;• allocates space for pointer to characters• can assign string constant to name– changes pointer

• must allocate space for charactersname = malloc(size);

Page 8: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: string copy

char * scopy(char s[])• make a new copy of string s1. find length of s2. allocate new byte sequence of this length3. copy s element by element to new byte sequence4. return address of new byte sequence

Page 9: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: string copy#include <stdio.h>#include <stdlib.h>

char * scopy(char s[]){ char * c; int l,i; l = slength(s); c = (char *)malloc(l); i = 0; while(i<l) { c[i] = s[i]; i = i+1; } return c;}

e.g. scopy(“hello”);

h e l l o \0s

Page 10: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: string copy#include <stdio.h>#include <stdlib.h>

char * scopy(char s[]){ char * c; int l,i; l = slength(s); c = (char *)malloc(l); i = 0; while(i<l) { c[i] = s[i]; i = i+1; } return c;}

h e l l o \0

e.g. scopy(“hello”);

s

c

Page 11: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: string copy#include <stdio.h>#include <stdlib.h>

char * scopy(char s[]){ char * c; int l,i; l = slength(s); c = (char *)malloc(l); i = 0; while(i<l) { c[i] = s[i]; i = i+1; } return c;}

h e l l o \0

e.g. scopy(“hello”);

s

c e l l o \0h

Page 12: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: string copymain(int argc,char ** argv){ char * s; s = scopy("water: the drink that is wet!"); printf("%s\n",s);}

$ scopywater: the drink that is wet!

Page 13: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Structure assignment

• can assign one structure to another– fields copied automatically– two identical versions in different memory sequences– copying

• can assign pointer to structure to pointer to structure– both pointers now refer to same memory sequence

• aliases

– sharing

Page 14: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: sort character counts

quicksort(struct freq a[],int l,int r){ int p; if(l<r) { p = partition(a,l,r); quicksort(a,l,p-1); quicksort(a,p,r); }}

Page 15: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: sort character countsint partition(struct freq a[],int l,int r){ int i,j,p; i = l; j = r; p = a[(i+j)/2].count; while(i<=j) { while(a[i].count<p)i = i+1; while(a[j].count>p)j = j-1; if(i<=j) { swap(a,i,j); i = i+1;j = j-1; } } return i;}

Page 16: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: sort character counts

swap(struct freq a[],int i,int j){ struct freq t; t = a[i]; a[i] = a[j]; a[j] = t;}

• copy whole struct from– a[i] to t– a[j] to a[i]– t to a[j]

Page 17: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Stack of struct v stack of pointers

• stack of entries– allocate more struct space than actually use– copy whole structs in swap

• instead:– allocate stack space for pointers to struct– copy pointers in swap

• must: – malloc space for structs– access fields from pointer

Page 18: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Structure space allocation

• for struct name:(struct name *) malloc(sizeof(struct name)• allocate for size of type• cast to pointer to type

Page 19: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Structure space allocation

• e.g.struct person {char * name;double height;int age;};struct person * pat;pat = (struct person *) malloc(sizeof(struct person));

pat

name height age

Page 20: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Structure pointer field access

exp->namei

(*exp).namei

• i.e. contents of offset of preceding elements from byte sequence that identifier points at

Page 21: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Structure pointer field access

• e.g.pat->name = “Pat”;pat->height = 1.68;pat->age = 23;

pat

name height age

1.68 23

aP t \0

Page 22: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

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

#define MAX 255

struct freq {int ch;int count;};

struct freq * f[MAX];int fp;

countchf

fp

Page 23: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

incFreq(int ch){ int i; i=0; while(i<fp) if(f[i]->ch==ch) { f[i]->count = f[i]->count+1; return; } else i = i+1;

Page 24: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

if(fp==MAX) { printf("more than %d different characters\n",MAX); exit(0); } f[fp] = (struct freq *) malloc(sizeof(struct freq)); f[fp]->ch = ch; f[fp]->count = 1; fp = fp+1;}

Page 25: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

showFreq(){ int i; i = 0; while(i<fp) { printf("%c:%d, ", f[i]->ch,f[i]->count); i = i+1; } printf("\n");}

Page 26: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

swap(struct freq * a[],int i,int j){ struct freq *t; t = a[i]; a[i] = a[j]; a[j] = t;}

Page 27: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

int partition(struct freq * a[],int l,int r)

{ int i,j,p; i = l; j = r; p = a[(i+j)/2]->count; while(i<=j) { while(a[i]->count<p) i = i+1; while(a[j]->count>p) j = j-1;

Page 28: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 2

if(i<=j) { swap(a,i,j); i = i+1; j = j-1; } } return i;}

Page 29: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Recursive structures

• cannot directly define recursive structs• e.g. linked list

struct list{ int value; struct list next;};struct list l;

• l is allocated space for a struct list– space for an int– space for a struct list• space for an int• space for a struct list…

Page 30: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Recursive structures

• define recursive structures with pointers• end chains with NULLe.g. linked liststruct list{int value; struct list * next;};

nextvalue

NULL1 2 42

Page 31: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Recursive structures

e.g. binary treestruct node {int value; struct node * left; struct node * right;};

leftvalue

17

right

1 42

Page 32: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 3

• allocating stack space for more pointers then needed• replace stack with linked liststruct freq {int ch;int count; struct freq * next;};struct freq * fp;

‘a’ 42

fp

NULL‘-’ 123

Page 33: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 3

incFreq(int ch){ struct freq * f; if(fp==NULL) { fp = (struct freq *) malloc(sizeof(struct freq)); fp ->ch = ch; fp->count = 1; fp->next = NULL; return; }

• allocate first entry

Page 34: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 3

f = fp; while(1) if(f->ch==ch) { f->count = f->count+1; return; } else if(f->next!=NULL) f = f->next;

• if character found then increment count• if not at end then try next entry

Page 35: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 3

else { f->next = (struct freq *) malloc(sizeof(struct freq)); f->next->ch = ch; f->next->count = 1; return; }

} • at end so add new entry

Page 36: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Example: character frequency 3

showFreq(){ struct freq * f; f = fp; while(f!=NULL) { printf("%c:%d, ",f->ch,f->count); f = f->next; } printf("\n");}

Page 37: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Auto-increment

• exp++ • evaluate exp to address• get value from address• increment value– depending on type of exp

• update address with incremented value

• if in exp then return un-incremented value

• ++exp • evaluate exp to address• get value from address• increment value– depending on type of exp

• update address with incremented value

• if in exp then return incremented value

Page 38: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Auto-increment

• exp-- • evaluate exp to address• get value from address• decrement value– depending on type of exp

• update address with decremented value

• if in exp then return un-decremented value

• --exp • evaluate exp to address• get value from address• decrement value– depending on type of exp

• update address with decremented value

• if in exp then return decremented value

Page 39: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Auto-increment• for int/short/long/char variable

– ++/-- adds/subtracts one:

name++ postInc(&name);int postInc(int * name){ int t = * name; *name = *name+1; return t; }

++name preInc(&name);int preInc(int * name){ *name = *name+1; return *name; }

Page 40: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Pointer arithmetic

type * name;• arithmetic on name is in units of the size for type• i.e. increases/decreases an address

name++ name = name+ size for type• i.e. pointer has moved on one element

name-- name = name – size for type• i.e. pointer has moved back one element

Page 41: F28PL1 Programming Languages Lecture 9 Programming in C - 4

Pointer arithmetic

name+exp name = name + exp *size for type• i.e. pointer has moved on exp elements

name-exp name = name –exp * size for type• i.e. pointer has moved back exp elements

• hard to test pointers for address boundary errors...