Presentation on theme: "1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs."— Presentation transcript:
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs
C and C++ C evolved from BCPL and B. BCPL was developed by Martin Richards in 1967. Ken Thompson based B on BCPL and used it to write very early versions of UNIX. These were "typeless" languages. All data occupied same amount of memory, and programmer had to deal with data type differences. C developed in 1972 by Dennis Ritchie at Bell Lab. C++ developed by Bjarne Stroustrup in ealry 1980s at Bell Labs to support OO programming
Areas C/C++ differ from Java® Not all code in objects header files preprocessor Call by reference allowed Pointers and pointer arithmetic Array bounds checking Garbage collection, destructors, memory leaks Templates (parameterized data types) Automatic instances of objects within a scope implicit type conversion operator overloading multiple inheritance virtual and non virtual methods
Copy constructors in C++ In C++, objects are passed as copies in call by value By default, objects are copied "bitwise" Objects that allocate memory in their constructors (arrays, other objects) need a special constructor that is equivalent of the clone( ) method in Java® Copy constructor takes reference to object, and returns a new object of same class that is an independent copy. Copy constructors often get invoked implicitly. For example, a method that returns a new object will implicitly invoke copy constructor if one exists.
Why copy constructors? Object of class A, when constructed, includes an array. In a method, we declare a newObj is a local variable, so its destructor method will be called when method returns. Memory allocated by constructor will be returned to memory pool unless there is a copy constructor for class A that allocated new memory.
Constructors and Destructors, p.3 No memory management in main program orfunctions, as a goal in C++ –In C, memory was managed for each variable You had to remember to allocate it and free it when done, no matter where these events occurred. Dynamic memory errors are over 50% of C program errors –In C++, we build memory management into classes New only in constructors; delete only in destructors Application developer sees nearly automatic garbagecollection. She never uses new; creates new objects just by defining them: Student Joe, same as int i Class developer has control of garbage collection when needed –C++ garbage collection is tough on lists, trees, etc.
Memory Management Errors Deleting same memory twice is a bug –Hard to find! –Trick: Comment out all deletes if you have a weird bug in a program with dynamic memo If bug goes away, youre deleting some memory twice Not deleting memory when done is a leak –Leaking 100 bytes per call on a Web server with a million calls a day means buying GBs of memory and crashing regularly anyway
Research project class C++ will dynamically set Student pointers to Undergrad, Grad, SpecGrad to get the pay for each If we stored Student objects in StaffList instead of pointers, we would only have the base Student class data!
Other Items Exceptions essentially same as Java® –Try, throw, catch Standard template library (STL) with Vectors, Lists, Stacks, similar to Java® –Cannot inherit from STL classes Multiple inheritance supported C has two string types (with conversions) –Null terminated arrays (old) –String class Arrays are just a memory location (reference) –Array size is passed as separate argument –Very problematic (buffer overruns) Recursion supported as in Java®