(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
[[Category:ECE264]] [[Category:Programming]] [[Category:C]] [[Category:lecture notes]]
 +
 +
=Lecture 15, [[ECE264]], Spring 2012, Prof. Lu=
 +
----
 
Lecture 15 - John Ribeiro
 
Lecture 15 - John Ribeiro
  
Line 23: Line 27:
 
   return v;
 
   return v;
 
}
 
}
 +
  
 
void Vector_destruct ( Vecotr *v)
 
void Vector_destruct ( Vecotr *v)
Line 199: Line 204:
 
[3] [4] [5]
 
[3] [4] [5]
 
[6] [7] [8]
 
[6] [7] [8]
 +
============================================================================
 +
Huayi Guo section-1 Professor Niklas
 +
 +
/*Lecture307
 +
GDB is a best tool to debug your program
 +
 +
esc x
 +
gdb ENTER
 +
(there will be a '*' in front the line the gdb is at)
 +
 +
r  args
 +
(if no args just r will be fine)
 +
n -> next line
 +
s -> next step (for a line that is a function call, gdb will go to the function)
 +
 +
print variable/structure
 +
 +
c (continue)
 +
 +
info breakpoint name
 +
 +
bt (show the call stack)
 +
(may help you find out that the problem that actually happens earlier that cause the program crashed at the current line)
 +
 +
display varaible(print the varaible when the gdb goes to a new line)
 +
 +
*/
 +
 +
 +
 +
===============================
 +
Kevin Tan(0023987592), section#2  notes 02/28
 +
 +
 +
can_move_fn()
 +
{
 +
  check whether it is a brick;
 +
  check where you came from;
 +
  check whether *forward =1;
 +
 
 +
}
 +
 +
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;
 +
}
 +
 +
assert(x!=0) -> to stop the program...
 +
 +
 +
Notes- James Chen /////////////////////////////////////////
 +
 +
Pointer may be quicker because it will not be copied through many recursions
 +
Do not use large main functions
 +
each function should only perform one task
 +
or...
 +
each function should only take up one screen
 +
Type code that is structured properly, do not restructure later
 +
don't ignore warning messages
 +
 +
typedef struct
 +
{
 +
  int age;
 +
  char * name;
 +
} Person;
 +
 +
Person * Person_construct(int a, char * n)
 +
{
 +
  Person * p;
 +
  p = malloc(sizeof(Person)); // this is important when creating pointers to structures, or else will get a seg fault
 +
  p -> age = a; 
 +
  p -> name = malloc(sizeof(char) * strlen(n) + 1 ));
 +
  strcpy(p -> name,n); // do not want a shallow copy;
 +
  return p;
 +
}
 +
 +
void Person_destruct(Person *p)
 +
{
 +
  free( p-> name);
 +
  free( p );
 +
}
 +
 +
Assert
 +
Do not use assert
 +
Do not kill the program, handle problems
 +
Thinking about this while coding the solution will affect design positively
 +
 +
 +
Don't cheat on IPA
 +
 +
Review for exam
 +
 +
makefile
 +
recursion
 +
search
 +
file (text and binary)
 +
malloc and free
 +
structures (with pointers)
 +
pointers
 +
stack and heap
 +
2d array
 +
 +
We may need to draw callstacks
 +
Functions may already have headers that we will need to follow
 +
Syntax error or segmentation fault results in no partial credit
 +
 +
3d to 2d arrays
 +
arr2d[r][c] -> arr1d[row * numCol + c]
 +
----
 +
[[2012_Spring_ECE_264_Lu|Back to ECE264, Spring 2012, Prof. Lu]]

Latest revision as of 04:25, 11 July 2012


Lecture 15, ECE264, Spring 2012, Prof. Lu


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]

================================================================

Huayi Guo section-1 Professor Niklas

/*Lecture307 GDB is a best tool to debug your program

esc x gdb ENTER (there will be a '*' in front the line the gdb is at)

r args (if no args just r will be fine) n -> next line s -> next step (for a line that is a function call, gdb will go to the function)

print variable/structure

c (continue)

info breakpoint name

bt (show the call stack) (may help you find out that the problem that actually happens earlier that cause the program crashed at the current line)

display varaible(print the varaible when the gdb goes to a new line)

  • /


===================

Kevin Tan(0023987592), section#2 notes 02/28


can_move_fn() {

 check whether it is a brick;
 check where you came from;
 check whether *forward =1;
 

}

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;

}

assert(x!=0) -> to stop the program...


Notes- James Chen /////////////////////////////////////////

Pointer may be quicker because it will not be copied through many recursions Do not use large main functions each function should only perform one task or... each function should only take up one screen Type code that is structured properly, do not restructure later don't ignore warning messages

typedef struct {

 int age;
 char * name;

} Person;

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

 Person * p;
 p = malloc(sizeof(Person)); // this is important when creating pointers to structures, or else will get a seg fault
 p -> age = a;  
 p -> name = malloc(sizeof(char) * strlen(n) + 1 ));
 strcpy(p -> name,n); // do not want a shallow copy;
 return p;

}

void Person_destruct(Person *p) {

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

}

Assert Do not use assert Do not kill the program, handle problems Thinking about this while coding the solution will affect design positively


Don't cheat on IPA

Review for exam

makefile recursion search file (text and binary) malloc and free structures (with pointers) pointers stack and heap 2d array

We may need to draw callstacks Functions may already have headers that we will need to follow Syntax error or segmentation fault results in no partial credit

3d to 2d arrays arr2d[r][c] -> arr1d[row * numCol + c]


Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang