Revision as of 10:38, 28 February 2012 by Song110 (Talk | contribs)

Lecture 15 - John Ribeiro

  1. ifndef
  1. define SOMETHING
  1. endif

typedef struct {

 int x;
 int y;
 int z;

} Vector;

Vector * Vector_construct ( int a, int b, int c ) {

 Vector * v;
 v = malloc ( sizeof ( Vector ) ) ; 
 v -> x = a;
 v -> y = b;
 v -> z = c;
 return v;

}

void Vector_destruct ( Vecotr *v) {

 free ( v );

}

Advantages of Using Pointers

void Vector_double ( Vector *v ) {

 v-> x *= 2;
 v-> y *= 2;
 v-> z *= 2;

}

- Actually changes values in heap memory

VS.

void Vector_double2 ( Vector v ) {

 v.x *= 2;
 v.y *= 2;
 v.z *= 2;

}

- Changes the values but does changes are lost when function is returned

- A single function should contain one idea - A function should b4e no longer than ~30 lines

typedef struct {

 int age;
 char * name;

} Person;

Person * Person_construct ( int a, char * n ) {

 Person * p;
 p = malloc ( sizeof ( Person ) );
 p -> age = a;
 p -> name = malloc ( sizeof (char) * ( strnlen (n) + 1 ) );
 strcpy ( p -> name, n );
 return p;

}

void Person_destruct ( Person * p ) {

 free ( p->name);
 free (p);

}

- Do no use assertion - kills the program when a condition is not met

assert ( x != 0 )

/* If x is 0, the program is killed */


--

On IPA 1-2

If you use #ifndef, you must use #define otherwise, you defeat the purpose of #ifndef

February 28

Each func should only do ONE thing Points will be taken if your program has “gigantic” functions (i.e. everything is in main)

If you malloc in your constructor, you must create a destructor.

Example Time!

void Vector_double(Vector v) {

 v->x *= 2;
 v->y *= 2;
 v->z *= 2;

}

void Vector_double2(Vector v) {

 v.x *= 2;
 v.y *= 2;
 v.z *= 2;

}

In the first case, the attributes are pointers. In the second case, the attributes are copy. In the copy, nothing happens after you leave the function. As a pointer, you can change the attributes separately. Also, if you have a large complex program it will also take very long to run the program because it must copy all of the attributes in the second case.

The constructor-destructor is symmetric. Meaning: malloc first, free last.

Controversy of Assert

Don’t kill the program. Handle problems. The idea of programming is changing. It is different from 20-40 years ago. Bottom line: do not use assert();

Don’t cheat on your IPA. If you spent so much time trying to change your program, why don’t you use the time to make your own program. Plagiarism is not acceptable.


Exam Review Makefiles Recursion Search File I/O (text & binary) Memory (malloc/free) Structures (with/without pointers) Pointers Stack/Heap Functions 2-D Arrays Strings

Some code will be given; you must follow this code. Open notes: you can use anything on paper or your programs. You will not have time to studying during the exam. Make sure you know every exercise and programming assignment.

Feb 28 Notes by T.Gao

///////////////////// Lecture notes_2/28_Kailu Song

1. reminder 2/26

for structure using #ifndef VECTOR_H & #define VECTOR_H together

2. pointer inside a structure vector vector_construct2() {

 vector v;
 v.x = a;
 v.y = b;
 v.z = c;

} return v; void vector_destruct(vector*v) {free (v);} void vector_double(vector *v) {

 v->x *=2;
 v->y *=2;
 v->z *=2;

} void vector_double2(Vector v) {

 v.x *=2;
 v.y *=2;
 v.z *=2;

}

be aware of constructor and destructor should be symmetric malloc first and free last

3. topics we have before exam 1 Makefile,recursion,search,file(text and binary) malloc/free structure (with pointers) pointers stack/heap memory functions 2-d arrays strings

4. different between one-d and 2-d array arr2d[numRow][numCol] [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2] one-d [0] [1] [2] [3] [4] [5] [6] [7] [8]

Alumni Liaison

Abstract algebra continues the conceptual developments of linear algebra, on an even grander scale.

Dr. Paul Garrett