Download presentation
Presentation is loading. Please wait.
Published bySamantha Burns Modified over 9 years ago
1
F28PL1 Programming Languages Lecture 9 Programming in C - 4
2
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
3
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
4
Dynamic Space Allocation char * malloc(int) allocates int bytes on heap returns address of 1 st byt like new in Java in stdlib
5
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 int s == 24 bytes
6
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
7
String Dynamic Space Allocation char * name; allocates space for pointer to characters can assign string constant to name – changes pointer must allocate space for characters name = malloc( size );
8
Example: string copy char * scopy(char s[]) make a new copy of string s 1.find length of s 2.allocate new byte sequence of this length 3.copy s element by element to new byte sequence 4.return address of new byte sequence
9
Example: string copy #include 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”); hello\0 s
10
Example: string copy #include 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; } hello\0 e.g. scopy(“hello”); s c
11
Example: string copy #include 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; } hello\0 e.g. scopy(“hello”); s c ello\0h
12
Example: string copy main(int argc,char ** argv) { char * s; s = scopy("water: the drink that is wet!"); printf("%s\n",s); } $ scopy water: the drink that is wet!
13
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
14
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); }
15
Example: sort character counts 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; if(i<=j) { swap(a,i,j); i = i+1;j = j-1; } } return i; }
16
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]
17
Stack of struct v stack of pointers stack of entries – allocate more struct space than actually use – copy whole struct s in swap instead: – allocate stack space for pointers to struct – copy pointers in swap must: – malloc space for struct s – access fields from pointer
18
Structure space allocation for struct name: (struct name *) malloc(sizeof(struct name ) allocate for size of type cast to pointer to type
19
Structure space allocation e.g. struct person {char * name;double height;int age;}; struct person * pat; pat = (struct person *) malloc(sizeof(struct person)); pat nameheightage
20
Structure pointer field access exp -> name i (* exp ). name i i.e. contents of offset of preceding elements from byte sequence that identifier points at
21
Structure pointer field access e.g. pat->name = “Pat”; pat->height = 1.68; pat->age = 23; pat nameheightage 1.6823 aPt\0
22
Example: character frequency 2 #include #define MAX 255 struct freq {int ch;int count;}; struct freq * f[MAX]; int fp; countchf fp
23
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;
24
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; }
25
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"); }
26
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; }
27
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;
28
Example: character frequency 2 if(i<=j) { swap(a,i,j); i = i+1; j = j-1; } return i; }
29
Recursive structures cannot directly define recursive struct s 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 …
30
Recursive structures define recursive structures with pointers end chains with NULL e.g. linked list struct list{int value; struct list * next;}; nextvalue NULL1242
31
Recursive structures e.g. binary tree struct node {int value; struct node * left; struct node * right;}; leftvalue 17 right 142
32
Example: character frequency 3 allocating stack space for more pointers then needed replace stack with linked list struct freq {int ch;int count; struct freq * next;}; struct freq * fp; ‘a’42 fp NULL‘-’123
33
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
34
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
35
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
36
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"); }
37
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
38
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
39
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 ; }
40
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
41
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...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.