(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...)
 
Line 1: Line 1:
Lecture 13 - February 21st - John Ribeiro
+
Lecture 14 - John Ribeiro
  
- Cannot compare two objects directly
+
typedef struct
 +
{
 +
  char *name;
 +
  int year;
 +
  int month;
 +
  int date;
 +
} Person;
  
* Exam 1 on 03/01 in class
+
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;
 +
}
  
**Online, open book
+
void Person_destruct ( Person p )
**File operation, structures, recursion, pointer...
+
{
**Will receive a zero if a segmentation fault is fount or syntax error
+
  free ( p.name );
 
+
}
For Makefile:
+
  
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
+
    ./ipa1 maze2 > output2
+
    diff output2 solution2
+
 
+
    valgrind --leak-check=yes -v ./ipa1 maze
+
 
+
**diff command in Linux will not return anything if no differences are found
+
 
+
-Many problems come from uninitialized variables and pointers. Unpredictable behaviour.
+
-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
+
 
+
  -Make memory allocation and release symmetric
+
  -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
+
 
{
 
{
   int x;
+
   return Person_constructor(p.name, p.year, p.month, p.date);
  int y;
+
}
  int z;
+
} Vector;
+
  
void vector_writeb ( char * filename, Vector v )
+
Person Person_assign ( Person p1, Person p2 )
 
{
 
{
   fptr = fopen ( filenamme, "wb" );
+
   Person_destruct (p1);
 
+
   return Person_copy(p2);
   /* "wb" means write binary and "rb" means read binary */
+
 
}
 
}
  
fwrite (address of object, size, number, file pointer)
+
--
  
if ( fwrite ( &v, sizeof(Vecotr), 1 , fptr ) != 1 )
+
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);
  
--NOTE: fwrite returns the number of items it writes
+
Person_destruct(p1);
 +
Person_destruct(p2);
 +
Person_destruct(p3);
  
OBJECT W/ POINTERS
+
Why constructor?
  
typedef dstruct
+
**A single place where all attributes are assigned
 +
**malloc in constructor
 +
  - destructor_copy, assign
 +
 
 +
typedef struct
 
{
 
{
  char *name;
 
 
   int year;
 
   int year;
 
   int month;
 
   int month;
 
   int date;
 
   int date;
} Person;
+
} DateofBirth;
  
Person Person_constructor ( char * n, int y, int m, int d )
+
typedef struct
 
{
 
{
   Person P;
+
   char *name;
  p.name =  malloc ( sizeof(char) * ( strlen(n) + 1 ) );
+
   DateofBirth dob;
   strcpy ( p.name, n );
+
} Person;
  p.year = y;
+
  
  return p;
+
* When Person returns, the compiler will copy attribute by attribute
}
+
  
MAIN:
+
If P3 = P1; // Shadow copy
  
Person P = Person_constructor ( "Alice", 1981, 11, 22);
+
A shadow copy is when two pointers are assigned the same value in memory i.e the point to the same place
Person_destruct(P);
+
  
void Person_destruct ( Person p )
+
int x = 24;
{
+
int y = x;
  free ( p.name );
+
x = 31
}
+
y = 31;
 +
 
 +
What is y?
 +
 
 +
Copy on Write - if one pointer changes memory

Revision as of 04:52, 28 February 2012

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

Alumni Liaison

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett