CS60 Final Review Notes ---------------------------- The final: - is Comprehensive - is 3 hours long - consists of - Essay questions - program analysis - program creation - multiple choice Main Concepts that will be tested - understanding of UNIX OS, UNIX usage and UNIX process model - understanding of the C language syntax and programming concepts - understanding of UNIX system calls - understanding of the C++ language syntax and concepts - understanding of the UNIX C and C++ development environment and tools UNIX OS, UNIX usage and UNIX process model ------------------------------------------- - OS Tasks - manage devices - handles file organization - process manager ex: what is the difference between the OS and the Windowing System (Xwindows)? - Process - key idea is that of a process, has a lifetime - FILE: - LOAD: - EXECUTE: - EXIT: ex: discuss the OS view of a process in terms of the parts of a process that exist in the EXECUTE stage. text seg data seg heap stack - Layout - many things in UNIX are tree based - process structure - file system structure - Shell - user interacts with OS through a shell process - command intepreter that looks for and executes executable files - special characters >, <, &, | - bash, csh, gosh, xterm is not a shell - ex: what is going on here? describe each word in the following command ls -al /etc | grep passwd > myfile the C language syntax and programming concepts ----------------------------------------------- - C programs can contain the following code blocks 1 - preprocessor directives 2 - definitions (prototypes/structs) 3 - variable declarations 4 - function declarations (main, foo) 5 - expressions 6 - control structures ex: what does #include mean? ex: why do we have to define functions and user defd data types at the top of the program? ex: where are variable declarations usually made? why? ex: what are functions? ex: how are expressions and statements different? ex: what is the difference between a control structure and a function? 3 - Variable Declarations - defn of variable: placeholder for data that can change during program execution - ALL VARIABLES HAVE THIS DEFINITION including pointers & structs - variables must be declared - memory reserved before variables used - variables are typed - determines how much memory is required - determines what ops can be performed - types can be converted - implicitly using C promotion scheme (smaller get promoted to larger) - explicitly using casting - variables can be operated upon using operators - binary math operators: +, -, *, /, =, ... - binary conditional operators: &&, ||, !, !=, == - unary operators: ++, --, * (deref), & (addr of) - no ternary operators!! - a == b == c IS LIKE SAYING a == (b == c) which is not what you expect 4 - Function Declarations - function declarations must match prototype definition - when function is called, values of params copied to new variables that exist for function duration - ALL PASSED IN VARS HAVE THIS PROPERTY, even pointers main() { int a, b; a = 4; b = foo(10, &a); } int foo(int one, int *two) { return(one - *two); } - can only return one value - ONLY have access to variables/memory inside function scope - all function vars deleted at end of function 5 - Expressions - expressions result in a number - expressions can contain - function calls - variables - operators on variables - constants - variables, operators, functions composed to form expressions - in C, FALSE=0, TRUE=nonzero - order of operations imposed by C on expression evaluation - math expressions usually make assignment at the end - a = - conditional expressions usually result in truth (1) or falsehood (0) - a = b && c; - a = b || (c && d); 6 - Control Structures - control the flow of your program, branching and looping - branching is making decisions based on expression results if (expr) then X else Y - looping is repeating a process until expression is true while(expr is true) do X - know syntax for - if/else, if/else if/else - for, while POINTERS ------------------------- - what is a pointer? - variable just like any other - variable contains addrs of other variables - special operator * used to dereference - modify variable contents outside function scope - UNIX system calls -------------------------- - programs use system calls to request OS intervention ex: what kind of things might a program ask of the OS? - interface to syscalls through function calls in C - picture of OS/processes/what happens when syscall - fork/exec/wait - open/close/read/write - pipe C++ language syntax and concepts ----------------------------------- - C++ is a superset of C, all C code should run as C++ program ex: why does C++ exist? - class - data hiding - operator overloading - friends - inheritance - virtual functions - templates - exception handling - STL - OO concepts: Excapsulation, Inheritance, Polymorphism - language concepts: convenience, generic coding, efficiency, code reuse ex: for each of the topics, match concepts that best match topic - low level differences - bool type - how IO is handled - streams: cin/cout - how memory is managed - new/delete - references (pointers sort of) - how are refs and pointers different? - Basic c++ facility is the class - used for encapsulation - inheritance - combines data and functionality into one OBJECT - data is hiden from user, access is through methods (safer) - private, protected, public member qualifiers - constructors, destructors - if you use pointers, ALMOST ALWAYS MUST PROVIDE construct/copy construct/destruct! - operator overloading - mostly for generic code, also for convenience - special 'operator' keyword - a + b - if a and b are of type 'foo', member function would be foo &operator+(foo &in); - exactly the same as a.+(b) - can be defined outside class def ex: why would we want to declare an overloaded op function outside class def? - friends ex: why do friends exist? - friendship must be given, not taken - inheritance - VERY powerful feature of C++/OO langs in general - provide hefty code reuse - provide generic code facilities - parent class defines general functions/data - derived classes inherit all parent functions and data - can override functions/data - can add new functions/data - derived variable instances have multiple types! - use this concept to provide generic code ex: what functions should NOT be considered inherited? why? - virtual functions - tackles inheritance problem where general function in parent class needs to call function specific to child types - pure virtual functions/virtual functions, difference? ex: why can't we have instances of classes that have pure virtual functions defined? - c++ IO - in C use file descriptors for syscalls, FILE * for formatted IO - in C++, what do we use? ex: why are stream objects organized using inheritance? - templates - ANOTHER way to do generic code, less OO, more for C++ efficiency constraint that there are built in types that are not objects ex: when do we NEED to use templates? - exception handling - let us program in the following mode: - create functions that work in ideal case - add exceptional cases later and allow programmer to decide how to handle them - what exceptions ARE NOT - fancy gotos - IE bad control structures - STL - standard template library - containers and algorithms - iterators - most containers have iterator notions - STL algorithms use iterators as params and return instead of data structs themselves. ex: why? UNIX C and C++ development environment and tools -------------------------------------------------- - overarching concept involves breaking up code into logical blocks and putting them in their own files - compiler phases preprocess compile assemble link ex: what is the input and output of each phase? .c -> preprocess -> modified .c mod .c -> compile -> assembly .s .s -> assemble -> object code .o .o -> link -> binary executable - we can archive compiled object code into libraries - *.a - this way we do not have to recompile everything evey time we use code that is unchanged ex: describe each of the words in the following command g++ -c foo.cpp -o foo.o -I./ g++ foo.o -o foo -L/usr/X11R6/lib -lglut ex: what is the make utility used for? ex: what is gdb?