CPSC 427: Object-Oriented Programming

Similar documents
CPSC 427: Object-Oriented Programming

Object-Oriented Principles and Practice / C++

8.1 The Roles of a Class

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Pointers and References

CPSC 427: Object-Oriented Programming

Operator overloading

CPSC 427a: Object-Oriented Programming

Study Guide to Exam 2

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CPSC 427: Object-Oriented Programming

Pointers and Dynamic Memory Allocation

CPSC 427: Object-Oriented Programming

CS 161 Exam II Winter 2018 FORM 1

Introducing C++ to Java Programmers

Vector and Free Store (Vectors and Arrays)

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

7.1 Optional Parameters

Object-Oriented Principles and Practice / C++

Vector and Free Store (Pointers and Memory Allocation)

Pointers in C/C++ 1 Memory Addresses 2

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

CPSC 427a: Object-Oriented Programming

Object-Oriented Programming for Scientific Computing

CSI33 Data Structures

Software Design and Analysis for Engineers

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Short Notes of CS201

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

CS201 - Introduction to Programming Glossary By


CS 103 Lab - Party Like A Char Star

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Variables, Memory and Pointers

Chapter 10 Introduction to Classes

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Week 8: Operator overloading

CPSC 427a: Object-Oriented Programming

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Introduction Of Classes ( OOPS )

Object-Oriented Principles and Practice / C++

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Lecture Notes on Memory Management

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Midterm Review. PIC 10B Spring 2018

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CS 103 Lab 6 - Party Like A Char Star

Assignment of Structs

Tokens, Expressions and Control Structures

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

377 Student Guide to C++

III. Classes (Chap. 3)

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

CMSC202 Computer Science II for Majors

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Chapter 17 vector and Free Store

Pointers, Dynamic Data, and Reference Types

Creating a String Data Type in C

a data type is Types

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1.

Memory and C++ Pointers

3. Simple Types, Variables, and Constants

Overloading Operators in C++

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Absolute C++ Walter Savitch

CPSC 427: Object-Oriented Programming

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

EE 355 Lab 4 - Party Like A Char Star

CS201- Introduction to Programming Current Quizzes

Lecture 2, September 4

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

University of Toronto

CPSC 427a: Object-Oriented Programming

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Guidelines for Writing C Code

Chapter 17 vector and Free Store. Bjarne Stroustrup

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Object Oriented Software Design II

CSE 333 Midterm Exam July 24, Name UW ID#

Dynamic Allocation of Memory

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Transcription:

CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 8 September 26, 2016 CPSC 427, Lecture 8 1/23

Storage Management (continued) Bar Graph Demo CPSC 427, Lecture 8 2/23

Storage Management (continued) CPSC 427, Lecture 8 3/23

Static data members A static data member belongs to the class itself rather than to instantiations of the class. Thus, all instantiations share the static member. Moreover, it exists even before the class has been instantiated. A static class data member must be declared and defined. It is declared by preceding the member declaration by the qualifier static. It is defined by having it appear in global context with an initializer but without the keyword static. It must be defined only once. CPSC 427, Lecture 8 4/23

Example from brackets demo In token.hpp, variable brackets is defined to be a static constant string: static const string brackets; It is initialized in token.cpp: const string Token::brackets = "[](){}<>"; CPSC 427, Lecture 8 5/23

Static data member example Here s a case where a non-const static data member is useful. In debugging a program with many objects of the same type, it is often useful to be able to distinguish the objects in diagnostic printouts. Using a static data member nextid, here s how to give each new object a unique ID: 1. Declare static int nextid; in the class. 2. Initialize it to 0 in the.cpp file. 3. Declare a non-static data member int uid = nextid++; CPSC 427, Lecture 8 6/23

Class definition class MyClass { static int nextid; int uid = nextid++; public: ostream& printid(ostream& out) const { return out << uid << endl; } }; In the.cpp file, you would place the line: int MyClass::nextID = 0; CPSC 427, Lecture 8 7/23

Static function members Function class members can also be declared static. As with static variables, the are declared inside the class by prefixing static. They may be defined either inside the class (as inline functions) or outside the class. If defined outside the class, the :: prefix must be used and the word static omitted. A static function can be called before the class has been instantiated. Example: Suppose MyClass defines static void instructions() { cout <<"To use..."; } It can be called using MyClass::instructions();. CPSC 427, Lecture 8 8/23

Static class functions are global Ways in which static class functions are like global C-style functions: 1. They do not take an implicit argument. 2. Their lifetime is the same as the lifetime of the entire program. 3. They may be called before the class has been instantiated. Ways in which they differ: 1. The name visibility is not restricted to the file in which the function is declared. 2. The name visibility is restricted by the privacy keywords private, protected, public. 3. The name must be qualified when called from outside the class (eg., MyClass::instructions()). CPSC 427, Lecture 8 9/23

Debugging memory management errors Memory management errors can be particularly difficult to debug because they often lead to bizarre symptoms that mislead debugging efforts. Debugging tools such as gdb have their place, but be aware that a program with memory management errors may behave differently when run under a debugger than when run alone. I ll give a brief overview of several kinds of errors that can happen, what causes them, and what to do about them. Read chapter 6 of the textbook for a much fuller discussion. CPSC 427, Lecture 8 10/23

Five common kinds of failures 1. Memory leak Dynamic storage that is no longer accessible but has not been deallocated. 2. Amnesia Storage values that mysteriously disappear. 3. Bus error Program crashes because of an attempt to access non-existent memory. 4. Segmentation fault Program crashes because of an attempt to access memory not allocated to your process. 5. Waiting for eternity Program is in a permanent wait state or an infinite loop. CPSC 427, Lecture 8 11/23

Memory leak A memory leak is when storage that has been allocated to a process becomes inaccessible. Symptoms are that the process s memory footprint becomes larger and larger over time, eventually leading the process to become very slow as useless data gets swapped in and out of physical memory. The tool valgrind will catch many kinds of memory errors, including memory leaks. To use, type valgrind myapp arg1 arg2... This runs myapp arg1 arg2... under its control. CPSC 427, Lecture 8 12/23

Amnesia I stored 7 in x. Why did cout << x print 356991? Answer: Somebody changed it between the time you stored into x and when you printed it. A few of many possible reasons: You no longer own the memory block x, and it is being reused for something else. Your program took a different path through your code than you expected. Some other part of your code was using memory that didn t belong to it, and it just happened to overwrite x. This can be caused by a buffer overrun error. CPSC 427, Lecture 8 13/23

Segmentation fault From the system s perspective, a memory reference can be one of three kinds: 1. The memory belongs to your process and you are permitted access. 2. The reference is to memory that exists, but you are not permitted to perform the requested operation on it. 3. A signal raised in response to various kinds of addressing errors detected by the hardware. Segmentation faults often result from attempting to dereference an invalid pointer, as in the following: int* xp = nullptr; cout << *xp << endl; CPSC 427, Lecture 8 14/23

Bus error Bus errors are less common in modern operating systems. Dereferencing 0 on older systems often caused bus errors. They can still happen in various situations, and you may occasionally still see them. The program mistakes that cause them are pretty much the same as the ones that cause segmentation faults. CPSC 427, Lecture 8 15/23

Waiting for eternity I wrote cout << x;, but nothing prints. Why? Some possibilities: Your output is in a buffer, waiting for a new line to be printed. Program control never reached your print statement. Your print statement was accidently commented out, perhaps because of an earlier missing */. Your program is in an infinite loop. Maybe there is no termination clause. Maybe you forgot to initialize a variable needed for termination. Maybe the termination condition simply never becomes true because of a logic error. CPSC 427, Lecture 8 16/23

What kinds of program errors cause these problems? delete is never executed, or it s executed twice. Dereferencing uninitialized pointer. Referencing a formerly valid pointer after its memory has been deleted or deallocated. Failing to initialize variables that control program logic. CPSC 427, Lecture 8 17/23

What makes these problems hard to diagnose? The effects of memory management errors often are only observed long after the original error, and often in an unrelated piece of code. References to unowned memory may return different values on different runs of the system, leading to seemingly random behavior. They may also return different values when run in a different environment, such as under the control of gdb or valgrind. Seemingly random behavior is almost always an indication of memory errors in your program. Finding the cause of the error requires methodical tracing of control flow through your program. CPSC 427, Lecture 8 18/23

Bar Graph Demo CPSC 427, Lecture 8 19/23

Overview of bar graph demo These slides refer to demo 08-BarGraph. This demo reads a file of student exam scores, groups them by deciles, and then displays a bar graph for each decile. The input file has one line per student containing a 3-letter student code followed by a numeric score. AWF 00 MJF 98 FDR 75... Scores should be in the range [0, 100] CPSC 427, Lecture 8 20/23

Overview (cont.) The output consists of one line for each group listing all of the students falling in that group. An 11 th line is used for students with invalid scores. Sample output: 00..09: AWF 0 10..19: 20..29: 30..39: PLK 37 40..49: 50..59: ABA 56 60..69: PRD 68 RBW 69 70..79: HST 79 PDB 71 FDR 75 80..89: AEF 89 ABC 82 GLD 89 90..99: GBS 92 MJF 98 Errors: ALA 105 JBK -1 CPSC 427, Lecture 8 21/23

Method Each student is represented by an Item object that consists of the initials and a score. The program maintains 11 linked lists of Item, one for each bar of the graph. A bar is represented by a Row object. For each line of input, an Item is constructed, classified, and inserted into the appropriate Row. When all student records have been read in, the bars are printed. A Graph object contains the bar graph as well as the logic for creating a bar graph from a file of scores as well as for printing it out. CPSC 427, Lecture 8 22/23

Analysis of 08-BarGraph demo main.cpp graph.hpp graph.cpp row.hpp row.cpp rownest.hpp item.hpp CPSC 427, Lecture 8 23/23