(New page: Lecture 13 - February 21st - John Ribeiro - Cannot compare two objects directly * Exam 1 on 03/01 in class **Online, open book **File operation, structures, recursion, pointer... **Will...)
 
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Lecture 13 - February 21st - John Ribeiro
+
[[Category:ECE264]] [[Category:Programming]] [[Category:C]] [[Category:lecture notes]]
  
- Cannot compare two objects directly
+
=Lecture 14, [[ECE264]], Spring 2012, Prof. Lu=
 +
----
 +
Lecture 14 - John Ribeiro
  
* Exam 1 on 03/01 in class
+
typedef struct
 +
{
 +
  char *name;
 +
  int year;
 +
  int month;
 +
  int date;
 +
} Person;
  
**Online, open book
+
Person Person_construct ( char * n , int y, int m, int d )
**File operation, structures, recursion, pointer...
+
{
**Will receive a zero if a segmentation fault is fount or syntax error
+
  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;
 +
}
  
For Makefile:
+
void Person_destruct ( Person p )
 +
{
 +
  free ( p.name );
 +
}
  
GCC = gcc -g -Wall -Wshadow
+
DEEP COPY (AVOID):
ipa1: maze.o main.o
+
      $(GCC)
+
...
+
...
+
  
test:
+
Person Person_copy ( Person p )
    ./ipa1 maze1 > output1
+
{
    diff output1 solution1
+
  return Person_constructor(p.name, p.year, p.month, p.date);
    ./ipa1 maze2 > output2
+
}
    diff output2 solution2
+
  
    valgrind --leak-check=yes -v ./ipa1 maze
+
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);
  
**diff command in Linux will not return anything if no differences are found
+
Person_destruct(p1);
 +
Person_destruct(p2);
 +
Person_destruct(p3);
  
-Many problems come from uninitialized variables and pointers. Unpredictable behaviour.
+
Why constructor?
-Use invalid values to guarantee programs fail
+
-Read your code before testing. Testing won't tell you where the program fails
+
-Design your solution on paper before writing code. The sooner you start coding, the later you can finish.
+
  
**Read code before testing
+
**A single place where all attributes are assigned
 
+
**malloc in constructor
  -Make memory allocation and release symmetric
+
   - destructor_copy, assign
  -Have only one place to allocate and release memory
+
  -Keep functions short
+
  -Do not copy and paste code. Use a function instead
+
   -Use valgrind, even though program seems to work
+
  
 
typedef struct
 
typedef struct
 
{
 
{
   int x;
+
   int year;
   int y;
+
   int month;
   int z;
+
   int date;
} Vector;
+
} DateofBirth;
  
void vector_writeb ( char * filename, Vector v )
+
typedef struct
 
{
 
{
   fptr = fopen ( filenamme, "wb" );
+
   char *name;
 +
  DateofBirth dob;
 +
} Person;
  
  /* "wb" means write binary and "rb" means read binary */
+
* When Person returns, the compiler will copy attribute by attribute
}
+
  
fwrite (address of object, size, number, file pointer)
+
If P3 = P1; // Shadow copy
  
if ( fwrite ( &v, sizeof(Vecotr), 1 , fptr ) != 1 )
+
A shadow copy is when two pointers are assigned the same value in memory i.e the point to the same place
  
--NOTE: fwrite returns the number of items it writes
+
int x = 24;
 +
int y = x;
 +
x = 31
 +
y = 31;
  
OBJECT W/ POINTERS
+
What is y?
  
typedef dstruct
+
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;
+
   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 year;
 
   int month;
 
   int month;
   int date;
+
   int data;
 
} Person;
 
} Person;
 
+
Person Person_construct(char *n, int y, int m, int d)
Person Person_constructor ( char * n, int y, int m, int d )
+
 
{
 
{
 
   Person P;
 
   Person P;
   p.name = malloc ( sizeof(char) * ( strlen(n) + 1 ) );
+
   P.name = malloc(sizeof(char)*strlen(n)+1);
   strcpy ( p.name, n );
+
   (check if P.name is = NULL)
   p.year = y;
+
  strepy(P.name,n);
 
+
   P.year = y;
   return p;
+
  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);
  
MAIN:
+
HINT FOR IPA1-3:
 
+
Maze_traverse(Maze xmz, int from, int *forward)
Person P = Person_constructor ( "Alice", 1981, 11, 22);
+
Person_destruct(P);
+
 
+
void Person_destruct ( Person p )
+
 
{
 
{
   free ( p.name );
+
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;
 
}
 
}
 +
----
 +
[[2012_Spring_ECE_264_Lu|Back to ECE264, Spring 2012, Prof. Lu]]

Latest revision as of 05:25, 11 July 2012


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; }


Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang