Lecture 14, ECE264, Spring 2012, Prof. Lu
Lecture 14 - John Ribeiro
typedef struct {
char *name; int year; int month; int date;
} Person;
Person Person_construct ( char * n , int y, int m, int d ) {
Person p; p.name = malloc ( sizeof(char) * ( stnlen(n) + 1 ) ); strcpy(p.name, n ); p.year = y; p.month = m; p.date = d; return p;
}
void Person_destruct ( Person p ) {
free ( p.name );
}
DEEP COPY (AVOID):
Person Person_copy ( Person p ) {
return Person_constructor(p.name, p.year, p.month, p.date);
}
Person Person_assign ( Person p1, Person p2 ) {
Person_destruct (p1); return Person_copy(p2);
}
--
Person p1 = Person_construct ("alice", 1980, 10, 21); Person p2 = Person_construct ("genny", 1984, 2, 5 ); Person p3 = Person_copy (p1); p3 = Person_assign (p3, p2);
Person_destruct(p1); Person_destruct(p2); Person_destruct(p3);
Why constructor?
- A single place where all attributes are assigned
- malloc in constructor
- destructor_copy, assign
typedef struct {
int year; int month; int date;
} DateofBirth;
typedef struct {
char *name; DateofBirth dob;
} Person;
- When Person returns, the compiler will copy attribute by attribute
If P3 = P1; // Shadow copy
A shadow copy is when two pointers are assigned the same value in memory i.e the point to the same place
int x = 24; int y = x; x = 31 y = 31;
What is y?
Copy on Write - if one pointer changes memory
/////////////////////////////////////////////
LECTURE NOTES_2/23_Kailu Song
(WHY CONSTRUCTOR? a single place where all attributes are assigned) (malloc in constructor = copy+assign+destructor)
typed of struct {
char *name;(if structure don't have pointer can write pointer Person P3 = P1; if copy attribute by attribute will get shallow copy) int year; int month; int data;
} Person; Person Person_construct(char *n, int y, int m, int d) {
Person P; P.name = malloc(sizeof(char)*strlen(n)+1); (check if P.name is = NULL) strepy(P.name,n); P.year = y; P.month = m; P.date = d; return P;
} void Person_destruct(Person P) {
free(P.name);
} Person Person_coty(Person P) {
return Person_construct(P.name,P.year,P.month,P.data); (inside the construct function,there is a malloc, so create another heap memory)
} Person Person_assign(Person P1, Person P2) {
Person_destruct(P1); return Person_copy (P2);
} Person P1 = Person_construct("Amy",1980,10,21); Person P2 = Person_construct("Jennifer",1981,2,5); Person P3 = Person_copy(P1); P3 = Person_assign(P3,P2);(if change this line to P3 = Person_copy(P2),P2 will become leak memory become there are two mallocs) Person_destruct(P1); Person_destruct(P2); Person_destruct(P3);
HINT FOR IPA1-3: Maze_traverse(Maze xmz, int from, int *forward) {
if (canMove(east)) { forward = 1; from = west; Maze_traverse(...east); book leaping;(dead end) } if (canMove(south)) { forward = 1; from = west; Maze_traverse(...south); book leaping;(dead end) } if (canMove(west)) { forward = 1; from = west; Maze_traverse(...west); book leaping;(dead end) } if (canMove(north)) { forward = 1; from = west; Maze_traverse(...north); book leaping;(dead end) }
canMove { CHECK whether it is a brick; CHECK where you come from; CHECK whether *forward = 1; }