Revision as of 06:01, 21 February 2012 by Jribeir (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

Alumni Liaison

Followed her dream after having raised her family.

Ruth Enoch, PhD Mathematics