



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Spring 09 CSCI 4430, A Milanova/BG Ryder 1
Spring 09 CSCI 4430, A Milanova/BG Ryder 2
Structures and unions Arrays Strings Pointers
Spring 09 CSCI 4430, A Milanova/BG Ryder 3
p = malloc(sizeof(int)); free(p);
*int q = p;
*q = 1;
Spring 09 CSCI 4430, A Milanova/BG Ryder 4
Necessary to build linked structures such as linked lists
Spring 09 CSCI 4430, A Milanova/BG Ryder 5
Spring 09 CSCI 4430, A Milanova/BG Ryder 6
Spring 09 CSCI 4430, A Milanova/BG Ryder 7
Type tree for type of “a” in 1: array
pointerTo
int
Type constructors: array, pointerTo, →, struct. Conventions: The array type includes only element type (it does not include the bound, and the dimension is 1, and therefore, it is omitted). The struct type includes element names, element types, and element order. Spring 09 CSCI 4430, A Milanova/BG Ryder 8
typedef int (*PFB)(); // Type variable PFB: what type? struct parse_table { // Type struct parse_table: what type? char *name; PFB func; }; int func1() { ... } // Function func1: what type? int func2() { ... }
struct parse_table table[] = { // Variable table: what type? {"name1", &func1}, {"name2", &func2} };
PFB find_p_func(char *s) { // Function find_p_func: what type? for (i=0; i<num_func; i++) if (strcmp(table[i].name,s)==0) return table[i].func; return NULL; } int main(int argc,char *argv[]) { // Function main: what type? ... }
Spring 09 CSCI 4430, A Milanova/BG Ryder 9
Type tree for PFB: pointerTo
→
() int
Type tree for type of find_p_func:
English: a function that takes a pointer to char as argument, and returns a pointer to a function that takes void as argument and returns int as return value.
pointerTo
→
() int
→
pointerTo
char
Spring 09 CSCI 4430, A Milanova/BG Ryder 10
struct _chunk { // Type struct_chunk: what type? char name[10]; int id; }; struct obstack { // Type struct obstack: what type? struct _chunk chunk; struct _chunk (chunkfun)(); void (freefun) (); }; void chunk_fun(struct obstack *h, void *f) { // Function chunk_fun: what type? h->chunkfun = ( struct _chunk ()() ) f; } void free_fun(struct obstack *h, void f) { // Function free_fun: what type? h->freefun = (void ()()) f; } int main() { struct obstack h; chunk_fun(&h,&xmalloc); free_fun(&h,&xfree); ... }
Spring 09 CSCI 4430, A Milanova/BG Ryder 11
Type tree for type of field chunkfun:
pointerTo
struct _chunk: struct
id: int
→ ()
pointerTo
name: array
char
12
Spring 09 CSCI 4430, A Milanova/BG Ryder 19
c: array [1..10] of integer; m,n: integer; procedure R(k,j: integer) begin k := k+1; j := j+2; end R; … m := 5; n := 3; R(m,n); write m,n;
m n 5 3 6 5
Value update happens in storage of the caller while callee is executing
Output: 6 5
Spring 09 CSCI 4430, A Milanova/BG Ryder 20
Advantage: safe Disadvantage: inefficient
Advantage: more efficient Disadvantage: potentially unsafe due to aliasing Aliasing occurs when two or more different names refer to the same memory location Changes could be made through each name E.g., m in main , and parameter k in R are aliases during the call to R
Spring 09 CSCI 4430, A Milanova/BG Ryder 21
y: integer; procedure P(x: integer) begin x := x + 1; x := x + y; end P; … y := 2; P(y); write y;
x -->y
during the call, x and y are the same location!
Output: 6
y 2 3 6
Spring 09 CSCI 4430, A Milanova/BG Ryder 22
x 2
y 2
Output: 2
y: integer; procedure P(x: integer) begin x := x + 1; x := x + y; end P; … y := 2; P(y); write y;
Spring 09 CSCI 4430, A Milanova/BG Ryder 23
j, k, m: integer; procedure Q(a,b: integer) begin b := 3; m := m * a; end Q; ... s1: Q(m, k); ... s2: Q(j, j); ...
global-formal aliases: <m,a> <k,b> associations during call S1;
formal-formal aliases: <a,b> during call S2;
Spring 09 CSCI 4430, A Milanova/BG Ryder 24
25
c: array [1..10] of integer; m,n: integer; procedure R(k,j: integer) begin k := k+1; j := j+2; end R; … m := 5; n := 3; R(m,n); write m,n;
Error in procedure R : can’t use parameters which are uninitialized!
Spring 09 CSCI 4430, A Milanova/BG Ryder 26
27
c: array [1..10] of integer; m,n: integer; procedure R(k,j: integer) begin k := k+1; j := j+2; end R; … m := 5; n := 3; R(m,n); write m,n;
By Value-Result k j 5 3 6 5
Output: 6 5
Spring 09 CSCI 4430, A Milanova/BG Ryder 28
c: array [1..10] of integer; m,n: integer; procedure R(k,j: integer) begin k := k+1; j := j+2; end R; / set c[i] = i / m := 2; R(m, c[m]); write c[1], c[2], …, c[10];
What element of c has its value changed? c[2]****? c[3]****?
k j 2 2 (^3 )
Spring 09 CSCI 4430, A Milanova/BG Ryder 29
m:= m + 1 c[m] := c[m] + 2
m c[ ] 2 1 2 3 4 5 6 7 8 9 10 3 1 2 5 4 5 6 7 8 9 10
c: array [1..10] of integer; m,n: integer; procedure R(k,j: integer) begin k := k+1; j := j+2; end R; / set c[n] to n / m := 2; R(m, c[m]); write m,n;
Spring 09 CSCI 4430, A Milanova/BG Ryder 30
Expression: c[m] , f(x,y) , x+z , etc. Evaluation of the argument is deferred until needed; Textual substitution with name clashes resolved. Argument is evaluated in caller’s environment – the THUNK (closure) carries the environment Inefficient
Same as call by reference