Cs 160
Cs 160
[email protected] [email protected]
istockphoto
Course Website
Office Hours
websiteexplorer.info
Project Testing and Grading
● Test cases are included with the project files
● Always run your solution on these cases
● Will give you a very strong idea of your score
● You may want to write additional test cases to further
verify the correctness of your solution
Project 1
Enroll yourself as a student for
CS 160
Debugger Review
● The debugger is extremely helpful in this course
● We will work with complicated C++ programs
● We will write x86 assembly which works with memory
directly
● Most common debugger is gdb
● The LLVM debugger (Mac OS X) is called lldb
GDB (Gnu Debugger)
● Standard debugger for Linux operating systems
● Supports a wide variety of languages such as C++
● We can put breakpoint, trace back to the execution
sequence, print memory variables using GDB
● Once we run a GDB session for an executable, we can
easily debug it using various commands supported by
GDB
GDB Useful Commands
● gdb ./executableName to run an executable using GDB
● run to run the program inside the debugger at the gdb prompt.
● bt will run a backtrace, which will show you full call stack at this point
including all arguments to functions
● p x to print the value of variable x
● break functionName to set a breakpoint at the beginning of the function
functionName
● break sourcefile.cpp:linenum set breakpoints for specific statements
● clear functionName to clear a breakpoint
● quit to quit from GDB prompt
Pointer Review
● Variable which points to another location
● Value is a memory address
● Can point to an object or a basic data type
● In C++, the pointer type gives information about what type
of data it points to
● For example an int * (usually!) points to an int
Pointer Review (contd.)
● You can cast a pointer to a different type:
int *p; float *q = (float *)p;
● This means that we are going to treat the value that p
points to as a float instead of an int
● This is useful when working with class inheritance
● Can cast a child class pointer to parent class pointer,
every child is also instance of parent
Visitor Pattern
● A way to separate an algorithm from data structure on
which it operates
● Useful when traversing a possibly complex structure
● Keeps the structure unchanged
● The visitor models processes in itself
Project 1 and Visitor Pattern
● Data Structure : Tree
● Implemented Visitor: Print
● Partially Implemented Visitor: Sum
● Not Implemented Visitor: Max
● Our data structure (Tree) is visitable and It accepts
visitor
void visit_children(Visitor* v) {
for (std::vector<Node*>::iterator it = this->children.begin();
this->children.end(); it++) {
(*it)->accept(v);
}
}
void accept(Visitor* v) {
v->visitNode(this);
}
};
Visitors
● We have a visitor base class which has a definition of
visitNode method
● Whenever we need a new feature, we will add a new
visitor which will extend this base class and implement
visitNode method
● Implementation of visitNode method will be different
according to our feature requirement
Visitors (contd.)
● Important to remember:
○ You choose whether the visitor is going to go on exploring the
children nodes or do some calculation on the parent first, by
calling visit_children() properly!
○ For example, Print visitor first prints the current node and then
visits children
void PrintVisitor::visitNode(Node* node){
std::cout<<node->value<<" ";
node->visit_children(this);
}
Construction of Tree
input : (1 (2 (3)) (4) (5 (6) (7)))
output tree :
1
2 4 5
Let’s see the
3 6 7 construction on
white board