170 likes | 326 Views
CSC 237 - Data Structures, Fall, 2008. Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!. Who am I?. Dr. Dale E. Parson (a.k.a. Professor Parson ), http://faculty.kutztown.edu/parson/
E N D
CSC 237 - Data Structures, Fall, 2008 Welcome to Data Structures! Tuesday, September 2 uses Monday’s schedule!
Who am I? • Dr. Dale E. Parson (a.k.a. Professor Parson), http://faculty.kutztown.edu/parson/ • In a previous life I was an AT&T technician and then a Bell Labs software engineer for over 25 years. Also, I consult as Amplified Computing. • I have taught at Albright College, Millersville University and Lehigh University. • This is, in my opinion, the central course of the CSC curriculum. I have taught it at Albright and Millersville.
Who are you? • Wide-awake Kutztown University computer science students who want to learn to design and implement data structures and algorithms in C++ at 8:00 AM three days a week! • You have already taken CSC 125, CSC 136, Computer Science II, earning >= C. • This course extends the topics developed in CSC 135. Also covered are: concepts of data abstraction, encapsulation, recursion; search and sort methods; and simple data structures. (Ah, this is the fine print for CSC 136!)
What are we planning to do? • We will learn to build the data structures and algorithms that are at the core of substantial software systems. • Containers such as lists, tables, stacks, queues, trees • Sets and mappings from keys to values • Algorithms for sorting or otherwise organizing data • Algorithms and file I/O techniques for persistent storage • We will use modular construction techniques, with the assistance of C++ abstract classes. • We will use the standard template library (STL) for some projects.
References First day handout (syllabus) GNU make http://www.gnu.org/software/make/manual/ GDB http://www.gnu.org/software/gdb/documentation/ http://faculty.kutztown.edu/parson
An example makefile • makefile in ~parson/ DataStructures /lecture1/intro all: build TARGET = printdemo DEBUG = 1 include $(HOME)/makelib build: $(TARGET) $(TARGET): $(OBJFILES) $(CPPCC) $(OBJFILES) -o $@ clean: subclean /bin/rm -f $(TARGET) $(TARGET).out $(TARGET).dif test: $(TARGET) ./$(TARGET) a b c d e > $(TARGET).out diff $(TARGET).out $(TARGET).ref > $(TARGET).dif
makelib in my $HOME/ • makelib CPPCC= g++ CXXFILES := $(wildcard *.cxx) OBJFILES := $(subst .cxx,.o,$(CXXFILES)) DEFFLAGS = ifeq ($(DEBUG),1) DEBUGFLAG = -g else DEBUGFLAG = endif INCFLAGS = -I. CPPFLAGS= $(DEFFLAGS) $(INCFLAGS) $(DEBUGFLAG) %.o : %.cxx $(CPPCC) -c $(CPPFLAGS) $< subclean: /bin/rm -f *.o
Some other files in my $HOME/ • .bash_profile -bash-3.00$ cat .bash_profile export EDITOR=/usr/local/bin/vim export VISUAL=/usr/local/bin/vim alias vi=vim alias make=gmake • .vimrc -bash-3.00$ cat .vimrc set ai set ts=4 set sw=4 set expandtab set sta
Interface definitions split source along module / driver boundaries. • printargs.h defines the interface to its module #ifndef PRINTARGS_H #define PRINTARGS_H #include <stdio.h> /* Function: printargs Prints an array of strings to an output file. Parameters: outfile: output file for writing. stringv: array of \0-terminated strings to be printed. stringc: numbers of strings in stringv Return value: none Side Effects: none Preconditions: The outfile must be a non-NULL, open FILE. stringc must equal the number of strings in stringv. Each string in stringv must be terminated with a \0 character. Postconditions: none (no return values from this function). Invariants: none */ void printargs(FILE *outfile, const char **stringv, const int stringc); #endif
Implement the interface in a .cxx file. • printargs.cxx implements the module of printargs.h /* printargs.cxx -- Function implementations for printargs.h. CSC237, Fall, 2008, Dr. Dale Parson, Class 1 example. This is a simple demo library module that prints an array of strings to an output file. */ #include "printargs.h" void printargs(FILE *outfile, const char **stringv, const int stringc) { int i ; for (i = 0 ; i < stringc ; i++) { fputs(stringv[i], outfile); fputs("\n", outfile); } }
Invoke the interface from a client module or test driver. • printmain.cxx is the test driver /* printmain.cxx -- main function that invokes printargs. CSC237, Fall, 2008, Dr. Dale Parson, Class 1 example. This is a simple driver for a demo library module that prints an array of strings to an output file. */ #include "printargs.h" int main(int argc, const char *argv[]) { printargs(stdout, argv, argc); return 0 ; }
Makefile-driven testing • makefile is structured for testing -bash-3.00$ make clean test /bin/rm -f *.o /bin/rm -f printdemo printdemo.out printdemo.dif g++ -c -I. -g printargs.cxx g++ -c -I. -g printmain.cxx g++ printargs.o printmain.o -o printdemo ./printdemo a b c d e > printdemo.out diff printdemo.out printdemo.ref > printdemo.dif
GDB for debugging -bash-3.00$ gdb --args printdemo a b c (gdb) break main Breakpoint 1 at 0x107f4: file printmain.cxx, line 13. (gdb) break printargs Breakpoint 2 at 0x10780: file printargs.cxx, line 14. (gdb) run Starting program: /export/home/faculty/parson/UnixSysProg/lecture1/printdemo a b c Breakpoint 1, main (argc=4, argv=0xffbffc54) at printmain.cxx:13 • printargs(stdout, argv, argc); • (gdb) p argc • $1 = 4 • (gdb) p &argc • $2 = (int *) 0xffbffc34
GDB continued (gdb) cont Continuing. Breakpoint 2, printargs (outfile=0x20b20, stringv=0xffbffc54, stringc=4) at printargs.cxx:14 14 for (i = 0 ; i < stringc ; i++) { (gdb) step 15 fputs(stringv[i], outfile); (gdb) next /export/home/faculty/parson/UnixSysProg/lecture1/printdemo 16 fputs("\n", outfile); (gdb) next 14 for (i = 0 ; i < stringc ; i++) { (gdb) p i $3 = 0 (gdb) p &i $4 = (int *) 0xffbffb6c
C++ interfaces and implementation classes • ~parson/DataStructures/lecture1/oo_variant • We will walk through code and test execution in class. • C++ abstract classes are used as interfaces. • Derived C++ concrete classes provide implementation. • Client code, including test drivers, use an interface without coding dependencies on underlying implementation class.
Unified Modeling Language (UML) Class Diagram of oo_variant print_main printmain.cxx <<interface>> printargs_interface.h printargs_interfaceprintargs_interface.cxx printargs(outfile : file, stringv : string[], stringc : int) printargs_simpleimpl printargs_simpleimpl.h printargs_simpleimpl.cxx
Programming practices • Always check and handle error return codes! • malloc() and new are exceptions that we will discuss. • Error logs, exit codes and C++ exceptions are useful. • Buffer overruns are annoying and dangerous • See man page for gets(), for example. • Make malloc/free or new/delete symmetrical. • Always use { curly braces } for control blocks. • Use both healthy and degenerate tests. • Ignoring these rules will cost you points.