Passing Arguments and The Big 5

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
C++ Memory Overview 4 major memory segments Key differences from Java
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
More C++ Features True object initialisation
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
System Programming Practical Session 7 C++ Memory Handling.
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.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Motivation and Overview
Chapter 1 C++ Basics Review
Pointers Psst… over there.
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
Dynamically Allocated Memory
Chapter 14: More About Classes.
Pointers Psst… over there.
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CSC 253 Lecture 8.
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object Oriented Programming (OOP) Lecture No. 9
Dynamic Memory Copy Challenge
Pointers & Functions.
Indirection.
Dynamic Memory A whole heap of fun….
Constructors and Destructors
CISC/CMPE320 - Prof. McLeod
The Big 5 and Lists.
Resource Allocation and Ownership
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Chapter 9: Pointers and String
Pointers and dynamic objects
Dynamic Memory Copy Challenge
Pointers & Functions.
Passing Arguments and The Big 5
The Stack.
CS31 Discussion 1H Winter19: week 9
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Essential Class Operations
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Rule of Five.
Destructors, Copy Constructors & Copy Assignment Operators
CS 144 Advanced C++ Programming April 30 Class Meeting
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
More on C++ G Carl Evans.
Presentation transcript:

Passing Arguments and The Big 5

Passing Arguments by Value Pass to the function a copy of a variable Advantages Safe – the variable will not be changed in the calling context Disadvantages Slow – copying can be slow to copy a large objects as a variable Arrays Not passed by value

Passing Arguments by Pointer Pass to the function a copy of the address of a variable Advantages Flexible – can change the value of the object Fast – only need to copy a pointer not the whole object Disadvantages Unsafe – can change the value of the variable in the calling function Complex – the variable is a pointer rather then and needs * or -> to access Arrays Always passed as a pointer since the name of an array is a pointer

Reference Variables Not pointers C++ Reference Variables Can not be null or uninitialized C++ Reference Variables Declare like pointers but use & rather then * They "refer" to another variable, but act like a variable Must be initialized when created so can never be null int my_int = 7; int &my_ref = my_int; my_ref = 8; cout << my_int << endl;

Passing Arguments by Reference Pass to the function a reference to a variable Advantages Flexible – can change the value of the object Fast – only need to copy an address not the whole object Disadvantages Unsafe – can change the value of the variable in the calling function Confusing – can not tell the difference at the call site Const Ref Safe like value but fast like a pointer

How hard was week 8 code review assignment? Easy Moderate Challenging Unreasonable

How long did week 8 assignment take? Less than 3 hours 3 to 6 hours 6 to 9 hours 9 to 12 hours More than 12 hours

Return Values Advantages Disadvantages Safe – Will always work correctly Disadvantages Slow – If a large object must be copied it could be slow With C++11 usually avoided more so with C++14

Return Pointers Advantages Disadvantages Allocations – Allows returning of heap allocated objects Disadvantages Unsafe – returning a stack address no longer be valid after the return

Return Reference Advantages Disadvantages Use Fast – always fast since only copying an address Disadvantages Unsafe – returning a reference to a stack variable will no longer be valid after the return Use Returning values from outside the function

Classes – No Internal "new" Member variables Methods Constructor Rule of Zero

Case Study: StringHolder class StringHolder { std::string *string_; public: StringHolder() : string_(nullptr) {}; StringHolder(const char *initial_string); const char* c_str() const noexcept; void ChangeString(const char *input_string); };

Classes – Containers Use "new" Rule of Zero won't work Deep vs Shallow Copy

Rule of Three – Copy Assignment Operator StringHolder& operator=(const StringHolder &source)

Rule of Three - Copy Constructor StringHolder(const StringHolder &source)

Rule of Three - Destructor ~StringHolder()

Rule of Three vs Rule of Five Rule of five (move semantics new with C++11) Move Constructor Move Assignment Operator rvalue reference && Can bind to a temporary (rvalue)

Rule of Three - Move Constructor StringHolder(StringHolder&& source)

Rule of Three – Move Assignment Operator StringHolder& operator=(StringHolder&& source)