Line 174: Line 174:
 
Person p = person.construct("Alice", 1981,11,22);
 
Person p = person.construct("Alice", 1981,11,22);
 
Person_destruct(p);
 
Person_destruct(p);
 +
 +
===============================
 +
Kevin Tan(0023987592), section#2  notes 02/28
 +
 +
 +
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)*strlen(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);
 +
}
 +
 +
Person Person_copy(Person p)
 +
{
 +
  return Person_destruct(p.name,p.year,p.month,p.date)
 +
}
 +
 +
Person Person_assign(person p1, Person p2)
 +
{
 +
  Person_destruct(p1);
 +
  return Person.copy(p2);
 +
}

Revision as of 12:01, 8 April 2012

Lecture 13 - John Ribeiro - February 21st

- Cannot compare two objects directly

Example:

Vector m; Vector c;

  • / Vector here consists of three integers */

if ( m == c ) {

 //ERROR!

}

  • Exam 1 on 03/01 in class

- Online, open book - File operation, structures, recursion, pointer.. - Make sure to go over all previous notes, exercises, and assignments - Will receive a zero if a segmentation fault, syntax error occurs in grading - Infinite submissions

For Makefile ( which will be given on the exam )

GCC = gcc -g -Wall -Wshadow ipa1: maze.o main .o

     $(GCC)
     ...
     This remains the same as in all previous programs
     ...

test:

    ./ipa1 maze1 > output1
    diff output1 solution1
    ./ipa1 maze2 > output2
    diff output2 solution2
    valgrind --leak-check=yes -v ./ipa1 maze

The above test part of Makefile will make it so that ./ipa1-2 exmaze5 (or something to similar effect) will no longer have to be typed. All that has to be included is:

make test

And the above cold will run

- 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

Tips && Tricks

  • make memory allocation and release symmetric
  • have only one place to allocate and release memory
  • keep functions short
  • do no copy and paste code. Use function instead
  • use valgrind, even though program seems to work

typedef struct {

 int x;
 int y;
 int z;

} Vector;

Vector_construct Vector_destruct Vector_print

The above is how a structure should be divided so that the program and main function can look cleaner and easier to read

BINARY MODE

void vector_writeb ( char * filename, Vector v ) {

 fptr = fopen ( filename, "wb"/"rb" );

}

In the above situation, "wb xor "rb" can be used. "wb" means write binary while "rb" means read binary.

fwrite ( address of object, size, number, filepointer )

         &v         sizeof(Vector)                1       fptr
                    # of elements in an array

fread operates in the same fashion with exactly the same arguments

  • Object with Pointers*

typedef struct {

 char *name;
 int year;
 int month;
 int date;

}Person;

Person Person_Constructor ( char * n, int y, int m, int d ) {

 Person p;
 p.name = malloc ( sizeof ( char ) * strnlen ( n ) + 1 );
 strcpy ( p.name, n );
 p.year = y;
 p.month = m;
 p.date = d;
 return p;

}

Call stack of above...

Person.name Person.year Person.month Person.date d m y n

void Person_destruct ( Person p ) {

 free ( p.name );

}

Main:

Person p = Person_Constructor ( "Alice", 1981, 11, 22 ); Person_destruct ( p );


  • ///////////////////////////////////////////////////////////

Lecture 13_ notes_ Kailu Song 1. Announcement:

   The link to the class slides:https://docs.google.com/present/embed?id=dcch9sqd_404d4pbkbgv&size=l

2.debug details:

   >make test (instead the test line)
   diff output1 solution1 (tell you the different between the two files)
   be aware of segment fault (problem from the memeory)
                      infinite loop
                      recursion

3. binary file:

   open: fopen(filename,"wb");(write binary file)
                                     "rb" (read binary file)
   write: fwrite(address of the object, size,number,file pointer);
             fwrite(&v, sizeof(vector), 5(number of elements of an array), pointer);

4.what if there is an object with an pointer? type of 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)*strlen(n)+1);
       streoy(p.name.n);
       p.year = y;
    }return p;

void Person_destruct(person p) {

  free(p.name); 

} Person p = person.construct("Alice", 1981,11,22); Person_destruct(p);

===================

Kevin Tan(0023987592), section#2 notes 02/28


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)*strlen(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);

}

Person Person_copy(Person p) {

 return Person_destruct(p.name,p.year,p.month,p.date)

}

Person Person_assign(person p1, Person p2) {

 Person_destruct(p1);
 return Person.copy(p2);

}

Alumni Liaison

Prof. Math. Ohio State and Associate Dean
Outstanding Alumnus Purdue Math 2008

Jeff McNeal