CSI33 Data Structures

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

CSI33 Data Structures

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Assignment of Structs

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

Object-Oriented Principles and Practice / C++

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

EECE.3220: Data Structures Spring 2017

CPSC 427: Object-Oriented Programming

Memory management COSC346

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

C10: Garbage Collection and Constructors

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Programming, numerics and optimization

And Even More and More C++ Fundamentals of Computer Science

Lecture 18 Tao Wang 1

Chapter 10 Introduction to Classes

C11: Garbage Collection and Constructors

CS201 Some Important Definitions

CSCI 262 Data Structures. The Big 3. Copy Constructor. Destructor. Assignment Operator 3/4/ The Big 3

Classes and Pointers: Some Peculiarities

Classes and Objects. Class scope: - private members are only accessible by the class methods.

CPSC 427: Object-Oriented Programming

Introducing C++ to Java Programmers

CS 241 Honors Memory

Object-Oriented Programming for Scientific Computing

Pushing at the back 28/11/2018. Problem. Our linked list class

Object-Oriented Principles and Practice / C++

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

Short Notes of CS201

The Class Construct Part 2

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

CS201 - Introduction to Programming Glossary By

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

An Introduction to C++

CPSC 427: Object-Oriented Programming

CSI33 Data Structures

Base Component. Chapter 1. *Memory Management. Memory management Errors Exception Handling Messages Debug code Options Basic data types Multithreading

DYNAMIC MEMORY ALLOCATION ON REAL-TIME LINUX

Intermediate Programming & Design (C++) Classes in C++

Pointers. Developed By Ms. K.M.Sanghavi

Vector and Free Store (Pointers and Memory Allocation)

Object-Oriented Programming

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

C++ Object-Oriented Programming

Object Oriented Design

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CSC1322 Object-Oriented Programming Concepts

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

6.096 Introduction to C++ January (IAP) 2009

COMP6771 Advanced C++ Programming

System Programming. Practical Session 9. C++ classes

Project. C++: Smart Pointers. The Plan. Announcement. Memory Leak. Pointer Ownership. Today: STL 1 Wednesday: STL 2 Thursday: Smart Pointers

Link-Based Implementations. Chapter 4

Lecture 15a Persistent Memory & Shared Pointers

Object-Oriented Principles and Practice / C++

04-17 Discussion Notes

Launchpad Lecture -10

CA341 - Comparative Programming Languages

IBS Technical Interview Questions

Midterm Review. PIC 10B Spring 2018

Dynamic Allocation of Memory

Scope. Scope is such an important thing that we ll review what we know about scope now:

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

CSI33 Data Structures

Lecture 6 Dynamic Classes and the Law of the Big Three. Instructor: George Wolberg Department of Computer Science City College of New York

C++ 8. Constructors and Destructors

Constructors for classes

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

CSCI 123 Introduction to Programming Concepts in C++

OBJECT ORIENTED PROGRAMMING

ADT: Design & Implementation

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

During the course of writing the Matrix class we will cover some interesting C++ topics. Specically: constructors and destructors, operator

Classes. Christian Schumacher, Info1 D-MAVT 2013

Overview of C Feabhas Ltd 2012

G52CPP C++ Programming Lecture 13

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Written by John Bell for CS 342, Spring 2018

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

Object Oriented Programming COP3330 / CGS5409

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Review and Recursion

Value Semantics and Concept Based Polymorphism Sean Parent Principal Scientist Adobe Systems Incorporated. All Rights Reserved.

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Objects Managing a Resource

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

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

Sudoku-4: Logical Structure

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation

Mobile Application Programming. Objective-C Classes

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College November 6, 2017

Outline Outline 1 Chapter 10: C++ Dynamic Memory

Outline 1 Chapter 10: C++ Dynamic Memory

Proper Memory Management Classes which allocate memory must manage it properly. Default behavior of C++ is insufficient: The assignment operation will not perform a deep copy of objects using references (pointers). Rather, the pointers themselves will be copied, leading to shared memory. Shared memory requires reference counting to be properly deallocated. This is not built into C++, and is hard to program. C++ objects will not even deallocate memory they have allocated themselves unless made to do so by the programmer.

Proper Memory Management A class must deallocate memory it allocates. Each object, when its lifetime is over, should free any memory it has used. A class must copy, not share, referenced object data when an existing object is being assigned a value from another object. A class must copy, not share, referenced object data when a new object is being created using the value of another.

Destructor The destructor for a class is a special member function written to perform any cleanup work at the end of an object s lifetime. If no such special work is necessary, a destructor need not be provided. A destructor is required for dynamic memory classes to prevent a memory leak (an object must deallocate any memory it has allocated when it has done its work. The destructor s name is tilde ( ) followed by the class name. The destructor takes no parameters. The destructor is called automatically when an object goes out of scope. The destructor is called automatically when the delete operator is called for a pointer to an object in that class.

Copy Constructor When a constructor for a class is called with an existing object of the class as an actual parameter, the data members are copied into the memory allocated for the new object. In C++, this is the behavior of the default copy constructor. What will not happen automatically is a deep copy of the memory pointed to by data members which are pointers. To force a deep copy, the copy constructor must be explicitly defined for the class.

Assignment Operator When an existing object of a class is assigned a value which is an existing object of the class, the data members are copied into the memory of the assigned-to object. In C++, this is the behavior of the default assignment operator. What will not happen automatically is a deep copy of the memory pointed to by data members which are pointers. To force a deep copy, the assignment operator, operator=, must be explicitly defined for the class.

Example Dynamic Array Class: List class List { public: List(size t capacity=10); // constructor List(const List &a); // copy constructor ~List(); // destructor int& operator[](size t pos); // bracket operator List& operator=(const List &a); // assignment List& operator+=(const List &a); // += operator void append(int item); size t size() const { return size ; private: void copy(const List &a); void resize(size t new size); //larger array int *data ; // dynamic array size t size ; // size of dynamic array size t capacity ; ; // capacity of dynamic array

Example Dynamic Array Class: List List::List(size t capacity) { data = new int[capacity]; capacity = capacity; size = 0;

Example Dynamic Array Class: List List::~List() { delete [] data ;

Example Dynamic Array Class: List List::List(const List &list) { copy(list); void List::copy(const List &list) { size t i; size = list.size ; capacity = list.capacity ; data = new int[list.capacity ]; for (i=0; i<list.capacity ; ++i) { data [i] = list.data [i];

Example Dynamic Array Class: List List& List::operator=(const List &list) { if (&list!= this) { // deallocate existing dynamic array delete [] data ; // copy the data copy(list); return *this;

Reference Return Types Reference return types are allowed only when a function returns the address of data that will still be there after the function call is finished. (This disallows returning a reference to a local variable.) You can return a reference to a data member (attribute) of an object in the class to which the member function belongs. In the List class, this is done in the implementation of the [] operator to allow indexed data to be on the left side of an assignment statement, as in List l(3); l[0] = 0; l[1] = 1; l[2] = 2;

Reference Return Types inline int& List::operator[](size t pos) { return data [pos];

Memory Leaks When memory that has been allocated is not deallocated in some scenario, then each time the scenario occurs another chunk of memory becomes unavailable to the system which is trying to reuse memory. This can happen thousands or millions of times. As it gets harder to find available memory when it is needed, the operating system tries to use the disk drive to keep memory it is using (this is called paging or swapping). This slows the system down, since disk access is much slower than semiconductor memory. Eventually the system crashes. Memory leaks are hard to find and fix, which is why they exist in commercially sold software. Rebooting your PC every once in a while to refresh memory can work around this problem.

Memory Leaks // This code is incorrect { int *x; x = new int; *x = 3; x = new int; *x = 4; delete x;

Accessing Invalid Memory Accessing invalid memory means reading or writing on memory which has been deallocated (and reallocated for a different task). This leads to unpredictable and incorrect behavior in a program. Reading such data will produce garbage since the memory has been reallocated and reused by a different task running at the same time. Writing on such memory will cause the task which now uses it to crash, since its contents have been changed.

Accessing Invalid Memory int main() // This program is incorrect { int *y = new int; delete y; *y = 3; return 0;