COMP 345 Advanced Program Design with C 1

  • Slides: 69
Download presentation
COMP 345 - Advanced Program Design with C++ 1 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 1 Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 2: Various preliminary topics Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 2 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 2 Click to edit Master title style PRELIMINARY TOPICS Data types Variable declaration and initialization Pointers Type checking Type coercion Strings Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 3 Data types • Highly similar

COMP 345 - Advanced Program Design with C++ 3 Data types • Highly similar to Java data types • Basic types are not classes (like Java) • Pit trap: different compilers will have different ranges for most basic data types • Some programs potentially will behave differently across different platforms • Hence, lack of portability of C++ programs • User-defined data types using struct (as in C), as well as class (object-oriented programming) • Both are allowed in the same program • In fact, they are almost equivalent, but struct was kept for backward compatibility • A struct can have data members, methods, constructors, destructors, etc • One difference is that a struct sets its members as public by default Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 4 Data types: simple types size,

COMP 345 - Advanced Program Design with C++ 4 Data types: simple types size, range and precision Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 5 Data types: simple types size,

COMP 345 - Advanced Program Design with C++ 5 Data types: simple types size, range and precision Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 6 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 6 Click to edit Master title style literals Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 7 Data types: literals • Literals

COMP 345 - Advanced Program Design with C++ 7 Data types: literals • Literals • 2, 5. 75, ‘Z’, "Hello World“ • Considered "constants": can’t change in program • All literals have an inherent type that can be determined during lexical analysis • Like many other languages, C++ uses escape sequences for string literals: Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 8 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 8 Click to edit Master title style variable declarations Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 9 Variable declaration • A variable

COMP 345 - Advanced Program Design with C++ 9 Variable declaration • A variable can be declared for any type valid in the current scope. int x; double y; my. Class mc; • Multiple variables of the same type can be declared on the same declaration: int x, y, z; • Any declared name can be referred to thereafter in the scope in which it is declared. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 10 Variable initialization • Declarations can

COMP 345 - Advanced Program Design with C++ 10 Variable initialization • Declarations can include an optional initialization, which can use different syntactical forms: Type a 1 {v}; a 2 = {v}; a 3 = v; a 4(v); • All of these are widely used and apparently equivalent. • However: • Some are restricted to use in certain situations. • Only the first one is universally usable, and is actually safer, as it implicitly does some checking of the value passed versus the specified type. int a 1 = 1. 5; //allowed using truncation int a 1 {1. 5}; //not allowed, as type checking is enforced Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 11 Variable initialization • In C++,

COMP 345 - Advanced Program Design with C++ 11 Variable initialization • In C++, “initialization” is a general concept whereby variables are created with an initial value supplied at creation. • This is as opposed to the creation of variables without an initial value, which implies that a memory space is assigned to a variable, but the previously assigned content of the memory space is used. • Initialization is thus safer, but is slightly time-consumptive. • Initialization may seem a simple concept, but in C++ there is a proliferation of rules and special cases related to initialization. • Rules vary widely across different C++ standards. • Even though you may think that you use simple initialization, expect to be faced with special/obscure cases. • The following slides briefly describe the different kinds of initializations and some simple examples. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 12 Variable initialization • Default initialization:

COMP 345 - Advanced Program Design with C++ 12 Variable initialization • Default initialization: variable is constructed with no initializer. For basic types, the previous value held in the memory space is kept. For objects, the default constructor is called. int x; std: : string s; class. A *obj. Av 1 = new class. A; • Value initialization: variable is constructed with an empty initializer. For basic types, the value is zero-initialized. For objects, each member is value-initialized. int x{}; std: : string s{}; class. A obj. A = class. A(); Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 13 Variable initialization • Direct initialization:

COMP 345 - Advanced Program Design with C++ 13 Variable initialization • Direct initialization: variable is constructed using explicit constructor arguments. For basic types, no constructors, but constructor call syntax can be used. For objects, the corresponding constructor is called int x(4); std: : string s("hello"); class. A obj. A(value 1, value 2, value 3); • Copy initialization: Variable is created using the value(s) from an existing object of the same type, or uses a converting sequence if available. Applies only to named objects. std: : string s = "hello"; class. A obj. Av 1; class. A obj. Av 2(obj. Av 1); class. A obj. Av 3 = obj. Av 1; Copy initialization is implicitly used when passing and returning objects by value. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 14 Variable initialization • List initialization:

COMP 345 - Advanced Program Design with C++ 14 Variable initialization • List initialization: Initializes an object from a braced initialization list. std: : string s{'a', 'b', 'c’}; int n 1{1}; // direct-list-initialization int n 2 = {1}; // copy-list-initialization • Reference initialization: Binds a reference to an object. char& c = a[0] int i 1; int& iref = &i 1; Reference initialization is used implicitly when a value is passed by reference or a reference is returned from a function. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 15 Variable initialization • Aggregate initialization:

COMP 345 - Advanced Program Design with C++ 15 Variable initialization • Aggregate initialization: Initializes an aggregate from braced initialization list. It is list initialization but applied to aggregates. • Simply stated, an aggregate is either: • an array • an object of a class that has only public members and no constructors • Definition of an aggregate varies between standards. char a[3] = {'a', 'b'}; int i[3] = {1, 2, 3}; class aggr. A{ class aggr. B{ int a, b, c; int x, y; aggr. B b; } } aggr. A a 1 = {1, 2, 3, {4, 5}}; Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 16 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 16 Click to edit Master title style type checking type coercion Implicit type conversion sequences Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 17 Type checking, type coercion, type

COMP 345 - Advanced Program Design with C++ 17 Type checking, type coercion, type conversion • C++ uses a manifest typing strategy • Variables and values are assigned types explicitly in the source code • Values can only be assigned to variables declared as having the same type • However, C++ allows type coercion, i. e. implicitly or explicitly changing the type of variables or values • This loophole, among other things, makes C++ a weakly typed language • Type mismatches • General Rule: Cannot place value of one type into variable of another type int var = 2. 99; // 2 is assigned to var! • Only the integer part "fits", so that’s all that goes • Called "implicit type casting" or "automatic type conversion" • When using pointers or classes, much more problematic! Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 18 Explicit type casting • C++

COMP 345 - Advanced Program Design with C++ 18 Explicit type casting • C++ provides operators for explicit type coercion, or type casting static_cast<double>(int. Var) • Explicitly "casts" int. Var to double type double. Var = static_cast<double>(int. Var 1/int. Var 2); • Casting forces double-precision division to take place among two integer variables. • Equivalent in meaning to the following C syntax, even though the C++ cast operation is checked at compile time and is thus less prone to runtime errors double. Var = (double)int. Var 1/int. Var 2; Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 19 Explicit type casting • Different

COMP 345 - Advanced Program Design with C++ 19 Explicit type casting • Different kinds of explicit type casting operations: static_cast<Type>(expression) General-purpose type casting const_cast<Type>(expression) Cast-out “constantness” dynamic_cast<Type>(expression) Runtime-checked conversion of pointers and references within a single class hierarchy. Used for downcasting from a superclass to a subclass reinterpret_cast<Type>(expression) Implementation-dependent casting, performs a binary copy and assigns the new type to the resulting binary copied value. Highly unsafe and error-prone. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 20 Inheritance: upcasting and downcasting •

COMP 345 - Advanced Program Design with C++ 20 Inheritance: upcasting and downcasting • When dealing with classes and subclasses, one can declare objects of a supertype and manipulate them as one of its subclasses void display. Geometric. Object(Geometric. Object& g) { cout << "The radius is " << g. get. Radius() << endl; cout << "The diameter is " << g. get. Diameter() << endl; cout << "The width is " << g. get. Width() << endl; cout << "The height is " << g. get. Height() << endl; cout << "The area is " << g. get. Area() << endl; cout << "The perimeter is " << g. get. Perimeter() << endl; } Geometric. Object -----------area perimeter Circle ------radius diameter Rectangle ------width height • Problem: subclass members are undefined in superclass Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 21 Inheritance: upcasting and downcasting •

COMP 345 - Advanced Program Design with C++ 21 Inheritance: upcasting and downcasting • May want to use static_cast: void display. Geometric. Object(Geometric. Object& g) { Geometric. Object* p = &g; cout << "The radius is " << static_cast<Circle*>(p)->get. Radius() << endl; cout << "The diameter is " << static_cast<Circle*>(p)->get. Diameter() << endl; cout << "The width is " << static_cast<Rectangle*>(p)->get. Width() << endl; cout << "The height is " << static_cast<Rectangle*>(p)->get. Height() << endl; cout << "The area is " << g. get. Area() << endl; cout << "The perimeter is " << g. get. Perimeter() << endl; } • This successfully compiles, but will fail at runtime if the object passed was originally of a type that does not contain the members referred to in the code. • static_cast makes a static (compile-time) type cast, but correct runtime behavior cannot be verified. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 22 Inheritance: upcasting and downcasting •

COMP 345 - Advanced Program Design with C++ 22 Inheritance: upcasting and downcasting • Use dynamic_cast to downcast into a subclass • dynamic_cast works on pointers • Does runtime checking to verify that the cast is successful • Also deals with polymorphic types and virtual methods at runtime Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 23 Conversion sequences • When a

COMP 345 - Advanced Program Design with C++ 23 Conversion sequences • When a variable is declared, it may be initialized using a value that is not of the same type as the variable. • The compiler will attempt to find a type conversion sequence that enables it to convert the type of the value into the type declared for the variable. • Two things can be used in a conversion sequence: • conversion constructor: a constructor that takes a value of a type and creates an object of another type. A: : A(int){…} • conversion operator: a member operator that has the name of a type. A: : operator int(){…} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 24 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 24 Click to edit Master title style pointers Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 25 pointers • Variables contain a

COMP 345 - Advanced Program Design with C++ 25 pointers • Variables contain a specific value, e. g. , an integer. • A pointer variable contains the memory address of a portion of memory that in turn contains a specific value. • For any type T, T* is the type “pointer to T”, i. e. a variable of type T* can hold the address of an object of type T. int i = 99; int* p = &i; cout << *p << endl; i (int) 99 &i p (*int) • Two operators on pointers: • Dereferencing operator: *, e. g. *p refers to the object pointed to by the pointer. • Address operator: &, e. g. &i refers to the address of the first memory cell holding a value. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 26 pointers • Consider: int *p

COMP 345 - Advanced Program Design with C++ 26 pointers • Consider: int *p 1, *p 2, v 1, v 2; • p 1 and p 2 are now uninitialized pointers (often called “wild pointers”). • They point to a memory cell whose address corresponds to the value that was already in their cell before allocation. • Pointer assignment: p 1 = &v 1; • Sets pointer variable p 1 to "point to" variable v 1 • "p 1 equals address of v 1" • Or "p 1 points to v 1“ Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 27 pointers • Value assignments v

COMP 345 - Advanced Program Design with C++ 27 pointers • Value assignments v 1 = 0; *p 1 = 42; • p 1 and v 1 refer to same memory cell • Changing either the value pointed to by p 1 or the value of v 1 also changes the other variable’s value. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 28 pointers • Pointer assignment vs

COMP 345 - Advanced Program Design with C++ 28 pointers • Pointer assignment vs value assignment: int v 1 = 42; int v 2 = 9; int *p 2 = &v 2; int *p 1 = &v 1; • Pointer assignment: p 2 = p 1; • Assigns one pointer to another • "Make p 2 point to where p 1 points“ • Value assignment: *p 2 = *p 1; • Assigns "value pointed to" by p 1, to "value pointed to" by p 2 Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 29 pointers • Dynamic variables •

COMP 345 - Advanced Program Design with C++ 29 pointers • Dynamic variables • Require the use of pointers • Allocated with new operator, deallocated with the delete operator • Need to be allocated and destroyed explicitly while program runs • C++ does not have a garbage collector • Local variables • Declared within a function definition • Not dynamic • Allocated on the stack when code block is entered (e. g. function call) • Destroyed when code block is exited (e. g. function call completes) • Often called "automatic" variables • Allocation and deallocation controlled by the runtime system as functions are called • Uses a function call stack mechanism to automatically manage memory allocation/deallocation • If pointers are declared in a function • The pointer is managed as a local variable • The value pointed to is dynamically allocated/deallocated Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 30 Dynamic/automatic memory allocation: example int

COMP 345 - Advanced Program Design with C++ 30 Dynamic/automatic memory allocation: example int global = 99; int f 2(){ int f 2 x = 3; return (global/f 2 x); } int f 1(int p){ // calling this function // creates a memory leak int* f 1 x = new int(10); return (p + *f 1 x + f 2()); } int main(){ int x, y, z; y = 1; z = 2; for (int i = 1; i <= 10; i++){ x = f 1(y); } } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 31 pointers • The operator new

COMP 345 - Advanced Program Design with C++ 31 pointers • The operator new creates dynamically allocated values that can then be pointed to by pointer variables. • The value created is a nameless pointer value. • Allocated on the heap (also known as freestore) through the runtime system’s interaction with the operating system. • All dynamically allocated variables need to be carefully managed by the programmer. • C++ does not have garbage collection. • Dynamically allocated variables need to be allocated and deallocated manually • Similar to C’s malloc and free Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 32 pointers int *p 1, *p

COMP 345 - Advanced Program Design with C++ 32 pointers int *p 1, *p 2; p 1 = new int; *p 1 = 42; p 2 = p 1; Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 33 pointers *p 2 = 53;

COMP 345 - Advanced Program Design with C++ 33 pointers *p 2 = 53; p 1 = new int; *p 1 = 88; Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 34 pointers • If the type

COMP 345 - Advanced Program Design with C++ 34 pointers • If the type used as parameter is of class type: • Constructor is called for new object • Can invoke different constructor with initializer arguments: My. Class *my. Ptr; my. Ptr = new My. Class(32. 0, 17); • Can still initialize non-class types: int *n; n = new int(17); Concordia University //Initializes *n to 17 //That does NOT mean int is a class Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 35 pointers • Pointers are full-fledged

COMP 345 - Advanced Program Design with C++ 35 pointers • Pointers are full-fledged types • Can be used just like other types • Can be function parameters • Can be returned from functions • Example: int* find. Other. Pointer(int* p); • This function declaration: • Has "pointer to an int" parameter • Returns "pointer to an int" Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 36 pointers • Potential problem if

COMP 345 - Advanced Program Design with C++ 36 pointers • Potential problem if freestore runs out of memory • Older compilers: • Test if null returned by call to new: int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory. n"; exit(1); } • Most contemporary C++ compilers (C++98 and after) : • new throws exception bad_alloc try { int * myarray= new int[1000]; } catch (bad_alloc&) { cout << "Error allocating memory. " << endl; } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 37 pointers • To deallocate dynamic

COMP 345 - Advanced Program Design with C++ 37 pointers • To deallocate dynamic memory, use the delete operator • When value no longer needed • Returns memory to freestore • Example: int *p; p = new int(5); //allocate memory … //Some processing… delete p; //deallocate memory p = NULL; //prevents dangling pointer errors • Deallocates dynamic memory "pointed to by pointer p“ • p is then a “dangling pointer” that still points to its previously allocated value. • If not deleted before the variable goes out of scope, memory is not freed, which creates a memory leak. • Plus, dereferencing a dangling pointer leads to unpredictable results, ranging from getting a seemingly random value to a program crash. • Managing dangling pointers and deallocating dynamically allocated memory is a very important aspect of proper C++ programming. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 38 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 38 Click to edit Master title style pointer arithmetic Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 39 Pointer arithmetic • Can perform

COMP 345 - Advanced Program Design with C++ 39 Pointer arithmetic • Can perform arithmetic operations on pointers • Used to navigate arrays (covered later) • Example: int *d; d = new int[10]; • d refers to: address of new int[10] • d + 1 refers to: address of new int[10] + 1*sizeof(int) • d + 2 refers to: address of new int[10] + 2*sizeof(int) • d[i] == *(&d[0]+i) == *(d+i) Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 40 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 40 Click to edit Master title style pointers and const Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 41 Pointers and const • When

COMP 345 - Advanced Program Design with C++ 41 Pointers and const • When using pointers, there are two separate meanings/usages of const: • Specify the constantness of the pointer • Specify the constantness of the value pointed to. int x; int * p 1 = &x; const int * p 2 = &x; int * const p 3 = &x; // non-const pointer to non-const int // non-const pointer to const int // const pointer to non-const int * const p 4 = &x; // const pointer to const int Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 42 void, wild, dangling, and null

COMP 345 - Advanced Program Design with C++ 42 void, wild, dangling, and null pointers • void pointer: a pointer that is allowed to be pointing to a value of any type. void increase (void* data, int psize){ if ( psize == sizeof(char) ){ char* pchar; pchar=(char*)data; ++(*pchar); } else if (psize == sizeof(int) ){ int* pint; pint=(int*)data; ++(*pint); } } int main (){ char a = 'x'; int b = 1602; increase (&a, sizeof(a)); increase (&b, sizeof(b)); cout << a << ", " << b << 'n'; return 0; } Disadvantage: need to cast them specifically in order to use them. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 43 void, wild, dangling, and null

COMP 345 - Advanced Program Design with C++ 43 void, wild, dangling, and null pointers • wild pointer: a pointer that points to an arbitrary memory location. This is most often due to an uninitialized pointer declaration int *x; Here, x points to the address that corresponds to whatever value that already was in the memory space allocated to x. Dereferencing a wild pointer may lead to: • Segmentation fault: • pointing to an address that is not accessible by the program’s process • pointing to an address that contains read-only data • Arbitrary value: • pointing to a valid address that contains a valid but arbitrary integer value Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 44 void, wild, dangling, and null

COMP 345 - Advanced Program Design with C++ 44 void, wild, dangling, and null pointers • dangling pointer: a pointer that used to point to a valid memory area, that has now been potentially reassigned to another usage. int* func(){ int num = 1234; // num is local to func /*. . . */ return &num; // func returns pointer to num } void func(){ Class. A *obj. A = new Class. A(); /*. . . */ delete(obj. A); // *obj. A is deallocated. // obj. A is now a dangling pointer. // Dereferencing will still appear // to work, until the memory is used // for something else. . . } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 45 void, wild, dangling, and null

COMP 345 - Advanced Program Design with C++ 45 void, wild, dangling, and null pointers • null pointer: a pointer that points nowhere. int* x = nullptr; int* y = NULL; int* z = 0; Dereferencing a null pointer is a compilation error : safer pointer usage. Pointers should be initialized as null pointers. Dangling pointers should be assigned to null. Check for null pointer is then a good way to know if a pointer is valid or not. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 46 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 46 Click to edit Master title style references Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 47 References • Pointers are very

COMP 345 - Advanced Program Design with C++ 47 References • Pointers are very powerful, as they allow: • A variable to refer a value held by another variable. • A variable to refer to different values held by different variables in time. • Pass information around without having to copy it. • However, due to their power, pointers bring additional complexities: • Must use a special syntax (*, &, ->) • Possibility of dangling pointers, wild pointers, null pointers. • Pointers are unsafe, as their use may easily result in undefined behavior. • References are pointer variables that eliminate some of the disadvantages of pointers, at the cost of eliminating some of their power. • Pointer arithmetic cannot be applied to a reference. • Any operation applied to a reference is actually applied onto the variable it refers to, including assignment. • Hence, references must be initialized upon declaration and cannot be changed afterwards. • Furthermore, given a reference int& r {v 1}, &r returns a pointer to the object referred to by r. Thus, we cannot even have a pointer to a reference. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

Advanced Program Design with C++ 48 References • A reference is in fact an

Advanced Program Design with C++ 48 References • A reference is in fact an “alias” for a memory space. • Terminology: the reference is an alias to a referent (the value pointed to). • Both the reference and the referee represent the same value/object. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

Advanced Program Design with C++ 49 References • References are often used to pass

Advanced Program Design with C++ 49 References • References are often used to pass parameters: • Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

Advanced Program Design with C++ 50 References • Or even return a value. •

Advanced Program Design with C++ 50 References • Or even return a value. • In combination, passing a reference and returning a reference allows to pass an object, process it, then return it, allowing the returned reference to be acted upon. • Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 51 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 51 Click to edit Master title style smart pointers Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 52 Smart pointers • Pointers are

COMP 345 - Advanced Program Design with C++ 52 Smart pointers • Pointers are a very good example of a powerful C++ feature that is dangerous to use. • Very powerful and lean tool to use. • But leads to: • Memory leaks: if a pointer is declared in a function and not deleted, the value that is pointed to is not freed. • Dangling pointers: if a pointer is deleted, the pointer still points to the freed memory block. • Solution: use smart pointers • Reduce bugs • Retain similar efficiency and syntax • Control memory management by methods • auto_ptr (now deprecated), unique_ptr, shared_ptr, weak_ptr • Defined in the <memory> library Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 53 Smart pointers • Smart pointers

COMP 345 - Advanced Program Design with C++ 53 Smart pointers • Smart pointers are conceptually the same as pointers. • Implemented as a template class: • Class that contains a pointer of a variable type. • Implements the *, ->, = operators, constructor and destructor. template <class T> class auto_ptr { T* ptr; public: explicit auto_ptr(T* p = 0) : ptr(p) {} ~auto_ptr() {delete ptr; } T& operator*() {return *ptr; } T* operator->() {return ptr; } }; • Classes, templates and explicit constructors will be explained later. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 54 Smart pointers Therefore instead of

COMP 345 - Advanced Program Design with C++ 54 Smart pointers Therefore instead of writing… The programmer writes… void foo(){ My. Class* p(new My. Class); auto_ptr<My. Class> p(new My. Class); p->Do. Something(); delete p; } } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 55 Smart pointers • Here is

COMP 345 - Advanced Program Design with C++ 55 Smart pointers • Here is an example of code which illustrates the situation of a dangling pointer: My. Class* p(new My. Class); My. Class* q = p; delete p; p->Do. Something(); // Watch out! p is now dangling! p = NULL; // p is no longer dangling q->Do. Something(); // Ouch! q is still dangling! • Problem: p and q are both pointing at the same address space, which is problematic. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 56 Smart pointers • For auto_ptr,

COMP 345 - Advanced Program Design with C++ 56 Smart pointers • For auto_ptr, this is solved by setting its pointer to NULL when it is copied: template <class T> auto_ptr<T>& auto_ptr<T>: : operator=(auto_ptr<T>& rhs){ if (this != &rhs) { delete ptr; ptr = rhs. ptr; rhs. ptr = NULL; } return *this; } • This implementation prevents a memory space from being pointed to by two different auto_ptr. • But maybe we wanted that possibility. Using smart pointers brings its limitations. • There are different variations of smart pointers. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 57 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 57 Click to edit Master title style parameter passing value returning Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 58 Passing parameters and returning values

COMP 345 - Advanced Program Design with C++ 58 Passing parameters and returning values • Parameters can be passed to a function: • By value • A copy of the value is made, then the copied value is passed to the function. • For objects, the copy constructor is called by the runtime system to make the copy. • Thus, the value used in the function is local to the function. • Changing it in the function does not change the value passed from the calling function. • Pass by value cannot accept a parameter that is an expression. • By pointer • A copy of the pointer value is made, then passed to the function. • Thus, both functions are pointing to the same value; no copy of the value pointed to is made. • Changing the pointed value in the called function will change the value in the calling function. • By reference • Conceptually same as pass by pointer, except that the called function cannot change where the received pointer is pointing. • Drawback: cannot pass NULL , as a reference cannot be NULL • Advantage: can accept unnamed values resulting from the evaluation of expressions as parameters: void f(const T& t); called as f(T(a, b, c)), or f(a+b) • Call by constant reference is very often used to save memory consumption to pass parameters that are not to be changed locally. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 59 Passing parameters and returning values

COMP 345 - Advanced Program Design with C++ 59 Passing parameters and returning values class C { public: C() {cout << "C: : C(); " << endl; }; C(int) { cout << "C: : C(int); " << endl; }; C(C&) { cout << "C: : C(C); " << endl; } }; int main() { cout << "= = = = =" << endl; C c; cout << "= = = = =" << endl; cout << "address where c is stored : " << &c << endl; cout << "passing by value" << endl; pass_by_value(c); cout << "passing by reference" << endl; pass_by_reference(c); cout << "passing by pointer" << endl; pass_by_pointer(&c); cout << "passing/returning by value" << endl; c = pass_and_return_by_value(c); cout << "passing/returning by reference" << endl; c = pass_and_return_by_reference(c); cout << "passing/returning by pointer" << endl; c = *pass_and_return_by_pointer(&c); return 0; } = = = = = C: : C(); = = = = = address where c is stored : 0068 F 9 B 3 passing by value C: : C(C); = = = = = pass_by_value = address where val_c is stored: 0068 F 8 B 8 = = = = = passing by reference = = = = = pass_by_reference = address of object that ref_c refers to: 0068 F 9 B 3 = = = = = passing by pointer = = = = = pass_by_pointer = address of object pointed to by ptr_c: 0068 F 9 B 3 = = = = = passing/returning by value C: : C(C); = = = = = address where val_c is stored: 0068 F 8 B 8 C: : C(C); passing/returning by reference = = = = = address of object that ref_c refers to: 0068 F 9 B 3 = = = = = passing/returning = = = = = address of object = = = = by pointer = = = = pointed to by ptr_c: 0068 F 9 B 3 = = = = = Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 60 Passing parameters and returning values

COMP 345 - Advanced Program Design with C++ 60 Passing parameters and returning values void cout cout } void cout } Concordia University pass_by_value(C val_c) { << "= = = = =" << endl; << "= pass_by_value " << endl; << "= address where val_c is stored: " << &val_c << endl; << "= = = = =" << endl; pass_by_reference(C& ref_c) { << "= = = = =" << endl; << "= pass_by_reference " << endl; << "= address of object that ref_c refers to: " << &ref_c << endl; << "= = = = =" << endl; pass_by_pointer(C* ptr_c) { << "= = = = =" << endl; << "= pass_by_pointer " << endl; << "= address of object pointed to by ptr_c: " << ptr_c << endl; << "= = = = =" << endl; Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 61 Passing parameters and returning values

COMP 345 - Advanced Program Design with C++ 61 Passing parameters and returning values C pass_and_return_by_value(C val_c) { cout << "= = = = =" << endl; cout << "address where val_c is stored: " << &val_c << endl; // returning by value returns a copy of the local object (copied using the copy constructor). return val_c; } C* pass_and_return_by_pointer(C* ptr_c) { cout << "= = = = =" << endl; cout << "address of object pointed to by ptr_c: " << ptr_c << endl; cout << "= = = = =" << endl; // Here we return a pointer to the object. // // Returning a pointer the original object is not really useful (though it avoids making a copy of the object). // Really useful if we want to receive an object, do some processing using it, // and return a new object created locally that resides on the heap, // or sometimes receive a null pointer and return a pointer to newly created object. // // if (ptr_c) { // process and change the passed object // } else { // do some processing and create a new pointer to a C // } return ptr_c; } C& pass_and_return_by_reference(C& ref_c) { cout << "= = = = =" << endl; cout << "= address of object that ref_c refers to: " << &ref_c << endl; cout << "= = = = =" << endl; // here we return the original reference // // Use this if you want the calling function to use the function call as a value part of an expression. // The most famous example of pass-and-return-by reference is the implementation // of stream extraction/insertion operators (<</>>). // // Limitation: as a reference cannot be made to point to a new object or to be null, // passing/returning by pointer is often the appropriate solution. return ref_c; } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 62 Click to edit Master title

COMP 345 - Advanced Program Design with C++ 62 Click to edit Master title style strings Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 63 Strings • C++ provides following

COMP 345 - Advanced Program Design with C++ 63 Strings • C++ provides following two types of string representations: • C-style character strings • string class type introduced with Standard C++ • Many libraries would define their own string type • You will encounter many different ways of declaring/manipulating strings. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 64 C-style character strings • With

COMP 345 - Advanced Program Design with C++ 64 C-style character strings • With C-style character strings, strings are arrays of characters terminated by a null character (‘’) char greeting[6] = {'H', 'e', 'l', 'o', ''}; char greeting[] = "Hello"; • <cstring> provides many string manipulation functions • strcpy(s 1, s 2): copy string s 2 into string s 1 • strcat(s 1, s 2): concatenate string s 2 onto the end of string s 1 • strlen(s 1): returns the length of string s 1 • strcmp(s 1, s 2): returns lexical distance between s 1 and s 2 • strchr(s 1, ch): returns a pointer to the first occurrence of character ch in s 1 • strstr(s 1, s 2): returns a pointer to the first occurrence of string s 2 in string s 1 Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 65 Strings: example of C-style strings

COMP 345 - Advanced Program Design with C++ 65 Strings: example of C-style strings #include <iostream> #include <cstring> using namespace std; int main (){ char str 1[10] = "Hello"; char str 2[10] = "World"; char str 3[10]; int len ; // copy str 1 into str 3: PROBLEM!! strcpy(str 3, str 1); cout << "strcpy(str 3, str 1) : " << str 3 << endl; // concatenates str 1 and str 2: PROBLEM!! strcat(str 1, str 2); cout << "strcat(str 1, str 2): " << str 1 << endl; // total length of str 1 after concatenation len = strlen(str 1); cout << "strlen(str 1) : " << len << endl; return 0; } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 66 Standard C++ string class •

COMP 345 - Advanced Program Design with C++ 66 Standard C++ string class • The <string> standard C++ library provides a string class • Provides all the operations mentioned above, using a more natural objectoriented style. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 67 Standard C++ string class Concordia

COMP 345 - Advanced Program Design with C++ 67 Standard C++ string class Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 68 Standard C++ string class: example

COMP 345 - Advanced Program Design with C++ 68 Standard C++ string class: example #include <iostream> #include <string> using namespace std; int main (){ string str 1 = "Hello"; string str 2 = "World"; string str 3; int len ; // copy str 1 into str 3 = str 1; cout << "str 3 : " << str 3 << endl; // concatenates str 1 and str 2 str 3 = str 1 + str 2; cout << "str 1 + str 2 : " << str 3 << endl; // total length of str 3 after concatenation len = str 3. size(); cout << "str 3. size() : " << len << endl; // compare two strings cout << "str 1 is lexically " << (str 1. compare(str 2)) << " away from str 2" << endl; return 0; } Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020

COMP 345 - Advanced Program Design with C++ 69 References • Y. Daniel Liang,

COMP 345 - Advanced Program Design with C++ 69 References • Y. Daniel Liang, Introduction to Programming with C++ (Chapter 1, 13, 15), • • Peason, 2014. Bjarne Stroustrup, The C++ Programming Language (Chapter 6, 7, 11, 22), Addison-Wesley, 2013. Tutorials. Point. Learn C++ Programming Language (Chapter 17). cplus. com. <string> class documentation. cppreference. com. initialization. cplus. com. Pointers. learncpp. com. Returning by value, reference, and address. isocpp. org. References. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2007 -2020