Dynamic Memory Copy Challenge

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction to Programming Lecture 39. Copy Constructor.
Operator Overloading Fundamentals
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers. Overview  What are Pointers?  How to use Pointers?  Use of Pointers.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Copy Constructors Fall 2008 Dr. David A. Gaitros
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.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Hank Childs, University of Oregon
Pointers and Dynamic Arrays
Pointers & Arrays.
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers Psst… over there.
Memberwise Assignment / Initialization
Pointers Psst… over there.
Variables with Memory Diagram
Automatics, Copy Constructor, and Assignment Operator
Anatomy of a Function Part 2
understanding memory usage by a c++ program
Linked List Intro CSCE 121 J. Michael Moore.
Dynamic Memory A whole heap of fun….
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Anatomy of a Function Part 3
How Functions Work Part 1
Pointers & Objects.
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Input Validation CSCE 121 J. Michael Moore
Copy Constructor CSCE 121 J. Michael Moore.
Static in Classes CSCE 121 J. Michael Moore.
Dynamic Memory A whole heap of fun….
How Dynamic Memory Works with Memory Diagram
How Functions Work Part 2
Dynamic Memory A whole heap of fun….
Dynamic Memory Management
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Passing Arguments and The Big 5
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Pointers & Arrays.
COP 3330 Object-oriented Programming in C++
Dynamic Memory Copy Challenge
Pointers & Functions.
The Stack.
Pointers and References
Essential Class Operations
How Dynamic Memory Works with Memory Diagram
Dynamic Memory CSCE 121.
How Memory Leaks Work with Memory Diagram
Copy Constructor CSCE 121.
Presentation transcript:

Dynamic Memory Copy Challenge CSCE 121 J. Michael Moore

Assignment Output: (7, 3) 11 int a = 7; int b = 3; cout << "(" << a << ", " << b <<")"<<endl; int c; c = b; c = 11; cout << c << endl;

Assignment Expectation is that information is copied. Expectation is that changes to the copy do not affect the source.

Assignment Output: (7, 3) (7, 11) 11 int* d = new int(7); int* e = new int(3); cout << "(" << *d << ", " << *e <<")"<<endl; int* f; f = e; *f = 11; cout << *f << endl;

Copy happens with pointers! Pointer address is copied Unfortunately, both now point to the same memory address.

Shallow vs. Deep Copy Shallow Copy Deep Copy b ← a b is assigned with a Shallow Copy Deep Copy a 7 a 7 b b 7

Shallow Copy output identifier stack heap class TwoNums { int* num1; int* num2; public: TwoNums(int x, int y): num1(new int(x)), num2(new int(y)) {} // stuff }; int main() { TwoNums a(4, 7); TwoNums b = a; } num1 b 7 num2 num1 a num2 4 identifier stack heap

Deep Copy output identifier stack heap 7 class TwoNums { int* num1; int* num2; public: TwoNums(int x, int y): num1(new int(x)), num2(new int(y)) {} // stuff }; int main() { TwoNums a(4, 7); TwoNums b = a; } 4 num1 b 7 num2 num1 a num2 4 identifier stack heap

Assigning Class Objects By default each data member is copied. Pointer addresses are copied, i.e. shallow copy. After copy, each identifier has the same value(s) uses the same memory address Expectation is a deep copy. uses a different memory address We can ensure deep copy happens. We’ll see how soon!