Docsity

CSCI 4430: Last Class Notes on Pointers, Recursive Types, and Type Declaration in C - Prof, Assignments of Programming Languages

Toggle sidebar
bg1
1
Spring 09 CSCI 4430, A Milanova/BG Ryder 1
Announcements
HW4
Fixed a typo in Problem 4.
Due April 14th
Spring 09 CSCI 4430, A Milanova/BG Ryder 2
Last Class
Types
Composite types
Structures and unions
Arrays
Strings
Pointers
Type declaration (i.e., type construction) in C
Spring 09 CSCI 4430, A Milanova/BG Ryder 3
Last Class: Composite Types: Pointers
A variable or field whose value is a reference
to some object
In C: int *p;
Operations
Allocation and deallocation of objects on heap
p = malloc(sizeof(int)); free(p);
Assignment of one pointer into another
int *q = p;
Dereferencing of pointer
*q = 1;
Spring 09 CSCI 4430, A Milanova/BG Ryder 4
Last Class: Pointers: Recursive Types
A recursive type is a type whose objects may
contain objects of the same type
Necessary to build linked structures such as linked lists
Pointers are necessary to define recursive types in
languages that use the value model for variables:
struct cell {
int num;
struct cell *next;
}
Spring 09 CSCI 4430, A Milanova/BG Ryder 5
Last Class: Pointers: Recursive Types (ii)
Recursive types are defined naturally in languages
that use the reference model for variables:
class Cell {
int num;
Cell next;
Cell() { … }
…
}
Spring 09 CSCI 4430, A Milanova/BG Ryder 6
Last Class: Pointers and Arrays in C
In C, pointers and arrays are interoperable:
int n;
int *a;
int b[10];
1. a = b;
2. n = a[3];
3. n = *(a+3);
4. n = b[3];
5. n = *(b+3);
pf3
pf4
pf5

Partial preview of the text

Download CSCI 4430: Last Class Notes on Pointers, Recursive Types, and Type Declaration in C - Prof and more Assignments Programming Languages in PDF only on Docsity!

Spring 09 CSCI 4430, A Milanova/BG Ryder 1

Announcements

 HW

 Fixed a typo in Problem 4.

 Due April 14th

Spring 09 CSCI 4430, A Milanova/BG Ryder 2

Last Class

 Types

 Composite types

 Structures and unions  Arrays  Strings  Pointers

 Type declaration (i.e., type construction) in C

Spring 09 CSCI 4430, A Milanova/BG Ryder 3

Last Class: Composite Types: Pointers

 A variable or field whose value is a reference

to some object

 In C: int *p;

 Operations

 Allocation and deallocation of objects on heap

 p = malloc(sizeof(int)); free(p);

 Assignment of one pointer into another

 *int q = p;

 Dereferencing of pointer

 *q = 1;

Spring 09 CSCI 4430, A Milanova/BG Ryder 4

Last Class: Pointers: Recursive Types

 A recursive type is a type whose objects may

contain objects of the same type

 Necessary to build linked structures such as linked lists

 Pointers are necessary to define recursive types in

languages that use the value model for variables:

struct cell {

int num;

struct cell *next;

Spring 09 CSCI 4430, A Milanova/BG Ryder 5

Last Class: Pointers: Recursive Types (ii)

 Recursive types are defined naturally in languages

that use the reference model for variables:

class Cell {

int num;

Cell next;

Cell() { … }

Spring 09 CSCI 4430, A Milanova/BG Ryder 6

Last Class: Pointers and Arrays in C

 In C, pointers and arrays are interoperable:

int n;

int *a;

int b[10];

1. a = b;

2. n = a[3];

3. n = *(a+3);

4. n = b[3];

5. n = *(b+3);

Spring 09 CSCI 4430, A Milanova/BG Ryder 7

Last Class: Type Declaration in C

 What is the meaning of the following

declaration in C? Draw the type trees.

1. int *a[n]

2. int (*a)[n]

3. int (*f)(int)

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

Last Class: Type Declaration in C (ii)

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

Last Class: Type Declarations in C (iii)

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

Last Class: Type Declaration in C (iv)

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

Last Class: Type Declarations in C (v)

Type tree for type of field chunkfun:

pointerTo

struct _chunk: struct

id: int

→ ()

pointerTo

name: array

char

Control Abstraction:

Parameter Passing

Read: Scott, Chapter 8.1-8.

(notes cover 8.3)

12

Spring 09 CSCI 4430, A Milanova/BG Ryder 19

Call by Reference

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

 Argument must be an l-value; l-value into parameter

Output: 6 5

Spring 09 CSCI 4430, A Milanova/BG Ryder 20

Call by Value vs. Call by Reference

 Make sense for languages with value model for

variables

 Call by value

 Advantage: safe  Disadvantage: inefficient

 Call by reference

 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

Aliasing: Call by Reference

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

No Aliasing: Call by Value

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

More Aliasing with Call by Reference

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

More on Call by Reference

 What happens when someone uses an

expression argument for a call by reference

parameter?

 (2*x) ??

25

Call by Result

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!

 Parameter is copied out into the actual argument

when the subroutine exits

Spring 09 CSCI 4430, A Milanova/BG Ryder 26

Call by Result (ii)

 Assume we have subroutine

P(k,j: integer)

k and j are result parameters.

 What is the interpretation of P(m,m)?

 Assume parameter k has value 2 and j has

value 3 at end of P. What value is m on return?

27

Call by Value-Result

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

 Argument is copied in into the parameter at entry,

parameter is copied out into the argument at exit

Spring 09 CSCI 4430, A Milanova/BG Ryder 28

Call by Value-Result (ii)

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

Call by Name

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;

 An expression argument is not evaluated at call.

It is evaluated within the callee if needed.

Spring 09 CSCI 4430, A Milanova/BG Ryder 30

Call by Name

 Call by name (Algol 60)

 Case1: expression argument

 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

 Case2: variable argument

 Same as call by reference