Line 214: Line 214:
 
   return Person.copy(p2);
 
   return Person.copy(p2);
 
}
 
}
 +
 +
****************Rohan Khante Lecture Summary***************
 +
 +
 +
typedef struct
 +
{
 +
 
 +
  char *name;
 +
  int year;
 +
  int month;
 +
  int date;
 +
}Person;
 +
This structure is declared in the header file to make it available to all files.
 +
Typedef is a keyword and is used to assign alternative names to existing types. It is used to specify how a variable indicates something.
 +
Example
 +
typedef int distance; //here it is used to replace int type by "distance"(for convenience of the programmer)
 +
distance ToIndi; (ToIndi is an integer variable which will be used to store the distance to Indianapolis)
 +
In the above code Person is used to define a name for the class.
 +
Example- Person Tom; This will create a structure called Tom which will have name, year, month and date in it.
 +
 +
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;
 +
}
 +
This is used for constructing a Person variable according to the input feeded from the user. Since we are interested in creating a variable of type Person, Person is the return type for Person_Construct.
 +
Since we do not know the number of characters in "n" we have to use malloc. sizeof(char) finds the sizeof the char variable type. This is multiplied by [strlen(n) PLUS one] to allow for the terminating string character--- '\0'. Strcpy(NOT strcOpy) is a function in string.h(#include <string.h>) which will copy the entire string from "n" to p.name. After the remaining assignments "p" is returned.
 +
 +
 +
void Person_destruct(Person p)
 +
 +
  free(p.name);
 +
}
 +
ONLY p.name has to be freed since memory was allocated for the string in p.name.
 +
NOTE- malloc and free go hand in hand. If you "malloc" a space you should "free" it later on. This avoids memory wastage and in very large programs will avoid program crashes. Also you do not have to "free" the remaining variables since their size is already pre-determined and no malloc was used.
 +
 +
Person Person_copy(Person p)
 +
{
 +
  return Person_construct(p.name,p.year,p.month,p.date)
 +
}
 +
This is used to copy one construct p to another. Since copying construct means creating another construct one can return Person_construct with the parameters being the construct which you want to copy.
 +
 +
Person Person_assign(person p1, Person p2)
 +
{
 +
  Person_destruct(p1);
 +
  return Person.copy(p2);
 +
}
 +
This function is used to assign the properties of one construct to another. Here after the assignment we do not need the first construct and only have use for the second one. This calls for memory freeing for the first one which is done by the Person_Destruct function

Revision as of 15:38, 7 May 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);

}

                                • Rohan Khante Lecture Summary***************


typedef struct {

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

}Person; This structure is declared in the header file to make it available to all files. Typedef is a keyword and is used to assign alternative names to existing types. It is used to specify how a variable indicates something. Example typedef int distance; //here it is used to replace int type by "distance"(for convenience of the programmer) distance ToIndi; (ToIndi is an integer variable which will be used to store the distance to Indianapolis) In the above code Person is used to define a name for the class. Example- Person Tom; This will create a structure called Tom which will have name, year, month and date in it.

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;

} This is used for constructing a Person variable according to the input feeded from the user. Since we are interested in creating a variable of type Person, Person is the return type for Person_Construct. Since we do not know the number of characters in "n" we have to use malloc. sizeof(char) finds the sizeof the char variable type. This is multiplied by [strlen(n) PLUS one] to allow for the terminating string character--- '\0'. Strcpy(NOT strcOpy) is a function in string.h(#include <string.h>) which will copy the entire string from "n" to p.name. After the remaining assignments "p" is returned.


void Person_destruct(Person p) {

 free(p.name);

} ONLY p.name has to be freed since memory was allocated for the string in p.name. NOTE- malloc and free go hand in hand. If you "malloc" a space you should "free" it later on. This avoids memory wastage and in very large programs will avoid program crashes. Also you do not have to "free" the remaining variables since their size is already pre-determined and no malloc was used.

Person Person_copy(Person p) {

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

} This is used to copy one construct p to another. Since copying construct means creating another construct one can return Person_construct with the parameters being the construct which you want to copy.

Person Person_assign(person p1, Person p2) {

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

} This function is used to assign the properties of one construct to another. Here after the assignment we do not need the first construct and only have use for the second one. This calls for memory freeing for the first one which is done by the Person_Destruct function

Alumni Liaison

Recent Math PhD now doing a post-doctorate at UC Riverside.

Kuei-Nuan Lin