CS 230 Programming Languages

11 / 06 / 2020

Instructor: Michael Eckmann Today’s Topics • Questions? / Comments? • More about Pointers vs. Reference types • Solutions to dangling pointers and memory leaks • Start ++

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 Pointers

• Reference types in C/C++ and Java and C# – C/C++ reference type is a special kind of pointer type • Used when declaring parameters to a function so that it is passed by reference • Formal parameter is specified with an & • But inside the function, it is implicitly dereferenced. – Makes code more readable and safer • Can get the same effect with regular pointers but code may be less readable (b/c explicit dereferencing required) and less safe Pointers

• Reference types in C/C++ and Java and C# – C/C++ reference type is a special kind of pointer type • Would there be any use for passing by reference using a constant reference parameter (that is, one that disallows its contents to be changed)?

– Java • References replace C++'s pointers – Why? • Java references refer to class instances (objects). • No dangling references b/c implicit deallocation Pointers

• Reference types in C/C++ and Java and C# – C# • Has both Java-like references and C++-like pointers. • Pointers are discouraged --- methods that use pointers need to be modified with the unsafe keyword. Pointers

• Implementation of pointers – Pointers hold an address • Solutions to problem – Tombstones • A tombstone points to (holds the address of) where the data is (a heap-dynamic variable.) • Pointers can only point to a tombstone (which in turn points to the actual data.) • What does this solve? • When deallocate, the tombstone is set to nil. • Why aren't they used do you think? --- Know of any languages that use them? Pointers • Solutions to dangling pointer problem (continued) – locks-and-keys • Pointers and variables need different implementation for this method • Pointers are ordered pairs of an integer key and an address. • Heap-dynamic variables – include a header cell that stores a lock value – and storage cell(s) for the variable itself • During allocation a lock value is calculated and placed in the key portion of the pointer AND the header cell of the variable. • Key and header cell are compared when the pointer is dereferenced and if they're the same it's a legal reference otherwise it is an illegal reference (which causes run-time error.) Pointers

• Solutions to dangling pointer problem (continued) – Locks-and-keys (continued) • Multiple pointers may point to the variable but they all must have the same key. • When variable is deallocated (explicitly), the variable's header cell is changed to an illegal lock value (so no key will match it ever.) – Any other solutions you can think of to handle the dangling pointer problem? Pointers

• Solutions to dangling pointer problem (continued) – Any other solutions you can think of to handle the dangling pointer problem? • Don't allow programmer to explicitly deallocate heap-dynamic variables. Like Java and LISP. Also like C#'s references (but not like C#'s pointers.) Pointers

• Solutions to memory leakage problem – Reference counters • Store a reference counter for each memory cell whose value is the number of pointers currently pointing to it. • When pointers change value or are destroyed, the counter is decremented. It is also checked to see if it is zero. If it is, then the memory can be reclaimed. – Any drawbacks to this method? Pointers

• Solutions to memory leakage problem – Reference counters (continued) • Any drawbacks to this method? – Space – Time – Circular references Pointers • Solutions to memory leakage problem (continued) – Garbage collection • Every heap cell needs a field as a flag to indicate whether or not it contains garbage. • When want to reclaim memory the garbage collector will – 1. Set all the flags as “containing garbage” – 2. Go through the program and determine all the memory that is being pointed to --- and changes the flags for those cells as “not containing garbage” – 3. All the ones still marked as “containing garbage” are reclaimed. – All preceding discussion assumed that the heap cells were all the same size. Major difficulties arise when non-uniform size cells are used. Why? C++ vs. Java • In C++ but not in Java – can use pointers to memory locations – have a pointer to a reference to an object that has been returned to the heap (aka destroyed, deallocated) • dangling pointer problem • explicit programmer deallocation of memory allowed – memory leaks • no garbage collection – array indices are not checked • unpredictable behaviour when accessing array elements beyond end of array

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ vs. Java • In C++ but not in Java – allow use of uninitialized variables (causes runtime error) – allowed to not return a value from a non-void function – can overload operators • e.g. for a class I can overload the + operator to perform some operation on objects of that class. more flexible/writable when used, than requiring/calling a function. – can have global functions that are not part of any class

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ vs. Java • In C++ but not in Java – has a preprocessor which can be used for (among other things) conditional compilation • e.g. code to do if compiling under linux vs. compiling under windows – has the STL which contains things similar to what is found in Java Collections API – has templates whereas Java uses generics for same purpose

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ vs. Java • c++ has fewer compile time checks (which generate errors) than Java • c++ has fewer run time checks • c++ has no standard GUI library • c++ has fewer standard library functions vs. the large Java API • c++ is not as portable as Java, but there are guidelines out there to which one should try to adhere for more portable c++

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ from C • There are programming features leftover from C that can be used in c++. – c-style strings, c-style dynamic , c-style printing, etc. • we will look at the c-style way of doing things, but typically prefer the c++ way. • we need to know the C ways because they are part of valid c++ programs and we need to be able to read code not written by us that may use the C style stuff and sometimes we might want to call a C function in some C library ...

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 • Some types C++ – int, short, long – float, double, long double – any of the above numeric types can be prefaced with signed or unsigned – char (typically 8 bits) – wchar_t (wide = larger than a char) – bool (0 = false, 1 = true) • ints can be interpreted as bool (holdover from C which had no bool), 0=false, any other int is true. – void – enum (creates an integer type and named constants)

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ // Hello.cpp

#include using namespace std; int main() { cout << "Hello, world!" << endl; return 0; }

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ // #include logically inserts the code from the iostream file into this file // (Hello.cpp) // iostream is a system header file so we use the < > // if we wanted to include our own user-defined header file then we use " "

// int main() is the function declaration (all programs need one main) // can take 2 arguments if defined with them (int argc, char **argv) // returns an int

// cout is like System.out.println // << is an operator for writing // >> is an operator for reading // endl is a newline character

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ // note: cout and endl are in the std namespace (like a package in java) // without using namespace std, we could: // std::cout << "Hello, world!" << std::endl;

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ • The C++ specification does not specify the exact sizes of the types therefore they are implementation dependent. If you want to know the size of a type on a particular system, you can use the sizeof operator in C++ to get this info. • Let's see a program that uses this sizeof operator and prints the sizes of the various types. • You can write C++ programs in many different IDE's, including Eclipse, but you can also write code in a text editor and compile it at the command line like: • g++ mycode.cpp -o mycode.o • then to execute your program do: • ./mycode.o

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ • Like Java, – C++ has for, while and do-while loops, – C++ has if's and else's – C++ has switch statements with cases – C++ has same arithmetic operators – C++ has same relational operators

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ • >> and << when used on ints do a shift. • int x = 10; • x = x << 2; // shift left by 2 bits multiplies by 4 • cout << x << endl; // will be 40

• >> is a shift right (divides by 2 * right operand) • padding high bits with 0's?

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 • More types C++ – array • similar to Java – int nums[30]; // array of 30 ints, index starts at 0 – int ages[] = {21, 18, 35, 52, 65}; – can't put brackets before the name (but in Java can)

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C / C++ • C style memory allocation – malloc / calloc – free • C++ style memory allocation – new – delete

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 • More types C++ – objects • a simple declaration in Java creates a reference to a class – Card a; // a is a Card reference • a simple declaration in C++ creates an object of that class by calling the default constructor – string a; // actually calls string's default constructor • string s1(“Hello”); // calls a different constructor • string s2 = string(“Bye”); // also calls a constructor • s1 = s2; // actually makes a COPY

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 • More types C++ – objects • string s1(“Hello”); // calls a different constructor • string s2 = string(“Bye”); // also calls a constructor • s1 = s2; // actually makes a COPY • s2 = “So long”; // doesn't affect s1

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 • More types C++ – objects/references – In Java we can have:

Card a; // a reference to a Card (does NOT call Card's default constructor) Card b = new Card(2, 2); a = b; // a and b now both refer to the same object b.setRank(5); // the ONE object has rank 5 and both a and b refer to it

– This is the typical behavior in Java for classes (except for Strings where = actually makes a “copy” of the data not an alias.)

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 • More types C++ – objects/references – In C++ we can have:

Card *a; // a pointer to Card (does NOT call Card's default constructor) Card *b = new Card(2, 2); a = b; // a and b now both refer to the same object b->setRank(5); // the ONE object has rank 5 and both a and b refer to it

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ • a struct is a record in C++ and in C e.g. struct PhoneNumber { int areaCode; // first 3 digits, e.g. 518 int exchange; // next 3 digits e.g. 580 int last4; // last 4 digits e.g. 5294 } • if you know structs from C, they are different in C++. In C++ they are essentially the same as classes except that by default a struct's members are public and by default a class's members are private.

Michael Eckmann - Skidmore College - CS 230 - Fall 2020 C++ • classes in C++ – can have multiple inheritance – typically divided into header and implementation (usually in different files) – class definitions end with a semicolon – divided into sections public: private: protected: – inline functions - code in the definition, for speed (one liners) – header (interface) file vs. implementation file – prototypes of functions and classes must appear before use – extern (says variable is declared elsewhere and nothing more is allocated) – let's look at the Card.h and Card.cpp files

Michael Eckmann - Skidmore College - CS 230 – Fall 2020