Move Semantics CSCE 121.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
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.
Introduction to Programming Lecture 39. Copy Constructor.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Values, variables and types © Allan C. Milne v
Pointers & Dynamic Arrays Shinta P.
Copy Constructors Fall 2008 Dr. David A. Gaitros
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Pointers *, &, array similarities, functions, sizeof.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
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.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Learning Objectives Pointers as dada members
AKA the birth, life, and death of variables.
Motivation and Overview
Chapter 1 C++ Basics Review
Introduction to Classes
Class: Special Topics Copy Constructors Static members Friends this
CS Computer Science IB: Object Oriented Programming
Abstract Classes and Inheritence Operator over-loading
COMP 2710 Software Construction Pointers
Dynamic Memory CSCE 121 J. Michael Moore.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Pointers and Linked Lists
Pointer Basics Psst… over there.
Automatics, Copy Constructor, and Assignment Operator
Const in Classes CSCE 121 J. Michael Moore.
Introduction to Classes
Automatics, Copy Constructor, and Assignment Operator
Chapter 9: Pointers.
Linked List Intro CSCE 121 J. Michael Moore.
Return by Reference CSCE 121 J. Michael Moore.
The Assignment Operator
Copy Constructor CSCE 121 J. Michael Moore.
Copy Assignment CSCE 121 J. Michael Moore.
Operator overloading Dr. Bhargavi Goswami
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Lesson Subject Book Week 4 lesson 1 Class, copy-constructor
Chapter 9: Pointers and String
A simple function.
COP 3330 Object-oriented Programming in C++
Pointer Basics Psst… over there.
Dynamic Memory Allocation
Class: Special Topics 2 For classes using memory allocation
Linked List Intro CSCE 121.
Copy Assignment CSCE 121.
Copy Constructor CSCE 121.
Rule of Five.
CS 144 Advanced C++ Programming April 30 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
Copy Constructors and Overloaded Assignment
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Move Semantics CSCE 121

Rvalues and Lvalues Lvalues live beyond an expression E.g. variables Rvalues are temporary E.g. x = 7 + 3 The 10 resulting from 7+3 is an Rvalue.

Inefficiency vector<int> getReverse(const vector<int>& original) { vector <int> reversed; for (int i=original.size()-1; i >= 0; ++i) { reversed.push_back(original.at(i)); } return reversed; Somewhere in main… vector<int> k = {2, 4, 6, 8}; vector<int> r = getReverse(k);

Before C++11 Vector is constructed in the function and returned in a temporary object. The returned data is copied to the object getting the assignment. Then the returned object is destructed. vector<int> k = {2, 4, 6, 8}; vector<int> r = getReverse(k); // copy assignment

After C++11 Instead of copying… Pilfer / steal the dynamic parts from the temporary object and use in the object getting the assignment. Instead of making a temporary copy then destructing the temporary, Use what is already there and would be destructed anyway. So if we had a vector with 1,000,000 elements… instead of copying 1,000,000 elements (time consuming) Point to those existing 1,000,000 elements and unpoint the temporary object. Copying one pointer is much faster than copying 1,000,000 elements!

Move Constructor Declaration MyClass(MyClass&& source); && indicates that it is an Rvalue, i.e. a temporary object.

Move Constructor Definition MyClass::MyClass(MyClass&& source) { // pilfer dynamic resources from source // set source dynamic resources to nullptr } More details here: https://msdn.microsoft.com/en-us/library/dd293665.aspx

Move Assignment Declaration MyClass& operator=(MyClass&& source); && indicates that it is an Rvalue, i.e. a temporary object.

Move Assignment Definition This should look familiar! Recall Copy Assignment. MyClass& operator=(MyClass&& source) { if (this != &source) { // delete old data from this / lhs // pilfer resources from source // set pointers in source to nullptr } return *this; More details here: https://msdn.microsoft.com/en-us/library/dd293665.aspx

Recall Check for self-assignment Delete old data Allocate new memory Move Copy Assignment Move Copy Constructor Check for self-assignment Delete old data Allocate new memory Copy data from source Allocate new memory Copy data from source If we compare the copy assignment operator to the copy constructor, we see first of all that they take the same parameter: a const reference to the source object. And it we look at what they do, we see that the copy constructor is the same as the copy assignment operator, minus a couple of missing steps that aren’t needed. Now why would we not need to check for self-assignment and delete old data? ... Because the object is just being created, so there’s nothing to delete yet, and it can’t be equal to the source because it’s not initialized yet. Pilfer resources from source Set pointers in source to nullptr Pilfer resources from source Set pointers in source to nullptr

References http://blog.smartbear.com/c-plus-plus/c11-tutorial-introducing-the-move-constructor-and-the-move-assignment-operator/ http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html http://www.codeproject.com/Articles/397492/Move-Semantics-and-Perfect-Forwarding-in-Cplusplus https://msdn.microsoft.com/en-us/library/dd293665.aspx https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)