Shallow Copy Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Dynamic Time Warping (DTW)
Standard Template Library Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSc 352 Freeing Dynamically Allocated Memory Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
CSIE Dept., National Taiwan Univ., Taiwan
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Decision Trees Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Singly Linked Lists Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University 1.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
2016/6/41 Recent Improvement Over QBSH and AFP J.-S. Roger Jang (張智星) Multimedia Information Retrieval (MIR) Lab CSIE Dept, National Taiwan Univ.
Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
RuSSIR 2013 QBSH and AFP as Two Successful Paradigms of Music Information Retrieval Jyh-Shing Roger Jang ( 張智星 ) MIR Lab, CSIE Dept.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Binary Search Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
STL: Maps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Maximum Likelihood Estimate Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Simulation of Stock Trading J.-S. Roger Jang ( 張智星 ) MIR Lab, CSIE Dept. National Taiwan University.
Linear Classifiers (LC) J.-S. Roger Jang ( 張智星 ) MIR Lab, CSIE Dept. National Taiwan University.
From C to C++ Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Link-Based Implementations
From C to C++ Jyh-Shing Roger Jang (張智星)
Search in Google's N-grams
Quadratic Classifiers (QC)
DP for Optimum Strategies in Games
Query by Singing/Humming via Dynamic Programming
Debugging Memory Issues
CSE 374 Programming Concepts & Tools
This pointer, Dynamic memory allocation, Constructors and Destructor
Search in OOXX Games J.-S. Roger Jang (張智星) MIR Lab, CSIE Dept.
Copy Constructor CSCE 121 J. Michael Moore.
Copy Assignment CSCE 121 J. Michael Moore.
Destruction and Copying
The Standard Template Library
Circularly Linked Lists and List Reversal
Essential Class Operations
Queues Jyh-Shing Roger Jang (張智星)
National Taiwan University
Review Chapter 10 PPT for full coverage.
Destruction and Copying
Query by Singing/Humming via Dynamic Programming
Insertion Sort Jyh-Shing Roger Jang (張智星)
Examples of Time Complexity
Selection Algorithm Jyh-Shing Roger Jang (張智星)
Naive Bayes Classifiers (NBC)
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
Game Trees and Minimax Algorithm
Essential Class Operations
Duration & Pitch Modification via WSOLA
Copy Assignment CSCE 121.
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
Sorting Algorithms Jyh-Shing Roger Jang (張智星)
Storing Game Entries in an Array
SPL – PS3 C++ Classes.
Pre and Post-Processing for Pitch Tracking
Presentation transcript:

Shallow Copy Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University

2 Memory Allocation in Classes Common programming errors when using “new” for memory allocation in classes Default copy constructor and assignment operator are based on “shallow copy”, which leads to errors easily. We need to design our own constructor/operator. Default copy constructor invoked  Shared memory! Default assignment operator invoked  Share memory and memory leak!

3 How to Fix “Shallow Copy”? To fix the problem of “shallow copy” Define our own copy constructor Define our own assignment operator

4 Examples Which Fix “Shallow Copy” Examples shallowCopy00.cpp: Demo of shallow copy deepCopy00.cpp: Use new copy constructor deepCopy01.cpp: Use new assignment operator deepCopy02.cpp: Use both Lesson learned If a class allocates memory via “new” (or the likes, such as “malloc” or “calloc”), we should provide a new copy constructor and a new assignment operator to allocate new memory for the created copy.

5 Q & A Questions How to avoid the error message in shallowCopy00.cpp? If we use STL vectors, do we still the problem of “shallow copy”?  Please give examples and post to FB. Further studies How to check memory leak? Windows: Purify Unix/Linux: Valgrind Any other suggestions? How to avoid memory leak? Use STL (standard template library)