Revision as of 14:53, 27 February 2012 by Song110 (Talk | contribs)

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

Alumni Liaison

Have a piece of advice for Purdue students? Share it through Rhea!

Alumni Liaison