Introduction to C, C++, Java P.J. Drongowski 15 September 2004 Some history + C - Dennis Ritchie - Bell Laboratories 1972 - Predecessors: B (developed by Ken Thompson) and BCPL - Ken Thompson wanted to create computing environment -- led to UNIX - The C Programming Language: Brian Kernighan and Dennis Ritchie - Language was standardized in the mid-1980's (X3J11) - http://cm.bell-labs.com/cm/cs/who/dmr/chist.html + C++ - Bjarne Sroustrup - AT&T Bell Laboratories 1983-1985 - First called "C with Classes" - Inspired by object-orientation in Simula67 - Stronger type checking than C - Was initially implemented as a C++ to C translator - Standardized (ISO/IEC 14882) + Java - Origins began in project at BridgeSystems; delivered to Sun in 1991 - "Industrial strength Smalltalk written in C++" - Java is *not* Smalltalk - Team at Sun began development on Oak interpreter, leading to Java - May 1995 Sun publically announces Java - Bill Joy and Jim Gosling (Sun Microsystems) - Eliminate warts from C++, "run anywhere," security features (most importantly, references instead of pointers/addresses and no applet input/output) We will use C strings in this course + Why? - C strings are in general use everywhere; all programmers know C strings - C++ strings are supported through a library - C strings are supported by just a handful of library routines -- easy to learn - C++ string library has many functions -- more difficult to learn + What is a C string? - A one-dimensional array of characters - Indexed from zero like all C arrays - String is usually terminated by a null (zero) character + C is a dangerous language - What happens when a string is overrun? - Security issue: buffer overrun vulnerability - Cracker sends message that overruns a string array on the execution stack with the intention of overwriting the return address - Program transfers control to another part of message holding the infectious payload - Always check array bounds! /* * A fun C++ example using inheritance ("one from the crypt" :-) */ #include using namespace std ; class Register { private: unsigned int value ; public: Register() { value = 0 ; } void clear() { value = 0 ; } void load(int newValue) { value = newValue ; } unsigned int read() { return(value) ; } } ; class Counter : public Register { public: void increment() { load(read() + 1) ; } void decrement() { load(read() - 1) ; } } ; class ShiftRegister : public Register { public: void left() { load(read() << 1) ; } void right() { load(read() >> 1) ; } } ; int main(int argc, char *argv[]) { Register ir ; Counter pc ; ShiftRegister ac ; pc.clear() ; pc.increment() ; pc.increment() ; ac.load(0x80) ; ac.right() ; cout << "pc: " << pc.read() << endl ; cout << "ac: " << hex << ac.read() << endl ; }