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 receive a zero if a segmentation fault is fount or syntax error
For Makefile:
GCC = gcc -g -Wall -Wshadow ipa1: maze.o main.o
$(GCC)
... ...
test:
./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; int y; int z;
} Vector;
void vector_writeb ( char * filename, Vector v ) {
fptr = fopen ( filenamme, "wb" );
/* "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 )
--NOTE: fwrite returns the number of items it writes
OBJECT W/ POINTERS
typedef dstruct {
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) * ( strlen(n) + 1 ) ); strcpy ( p.name, n ); p.year = y;
return p;
}
MAIN:
Person P = Person_constructor ( "Alice", 1981, 11, 22); Person_destruct(P);
void Person_destruct ( Person p ) {
free ( p.name );
}