Reference Counting. Steven R. Bagley

Size: px
Start display at page:

Download "Reference Counting. Steven R. Bagley"

Transcription

1 Reference Counting Steven R. Bagley

2 Previously Objects are blocks of memory Store pointer to base of object s memory so we can reference it Memory needs to be released when the object is no longer needed Working out when to release memory is not trivial

3 Object Ownership Who deletes an object? An object must be deleted when it is no longer used But how can a piece of code know that it is the last piece of code using an object

4 Object Lifetime Can have multiple pointers to an object Can t delete the object until all pointers are out of scope How do we know when all pointers are out of scope?

5 Simple Case void AMethod() Foo *foo = new Foo(); foo->setfoo(42); foo->dostuff(); foo->setfoo(1963); foo->domorestuff(); delete foo; Easy to say when we delete here -- Code uses object, then when it is finished with deletes it Can we just say, what creates an object is responsible for destroying it No -- what happens if we give the pointer to something else

6 Problem Case void AMethod(Collection *acollection) Foo *foo = new Foo(); foo->setfoo(42); acollection->add(foo); delete foo; We have some sort of Collection representation that we add the object too At this point, we have too things pointing to the object. Our local variable foo, and the collection. If we follow, the creator deletes it paradigm then it is deleted on the next line. The collection now points to dead memory

7 Premature Deletion AMethod finishes and foo is deleted Object s memory is marked as unused Other bits of the code continue to use it and things work Eventually, memory will be reused And then it will break

8 Rules for deletion First attempt at a rule: What creates it, deletes it Very flawed rule Let s attempt to modify it: What creates it, deletes it unless it gives it to something else who is then responsible for deleting it

9 Problem Case void AMethod(Collection *acollection) Foo *foo = new Foo(); foo->setfoo(42); acollection->add(foo); Better, Collection s job to delete it now but...

10 Problem Case Collection *acollection = new Collection(); Foo *foo; AMethod(aCollection); /* */ foo = acollection->getchildat(2); delete acollection; /* foo now points to dead space */ foo->dostuff(); // Will crash eventually Collection deletes its children when it is deleted... This includes foo... There s a problem...

11 Deleting Objects Object should only be deleted when it is no longer used Many pointers to same object instance Difficult to know when it is safe to delete Leads to errors Complex code e.g. objects not deleted, objects deleted too early

12 Problem Case void AMethod(Collection *acollection) Foo *foo = new Foo(); foo->setfoo(42); if(somecondition) acollection->add(foo); else delete foo; Code becomes very messy An automated system for deletion would be very useful

13 I'm not allowed to say how many planes joined the raid, but I counted them all out and I counted them all back. Brian Hanrahan

14 Deletion Solution Object should be deleted when and only when the last pointer to it goes out of scope Know when pointers are copied Can also keep a count of everytime a pointer is copied And also when it goes out of scope

15 void AMethod(Collection *acollection) Foo *foo = new Foo(); foocounter = 1; // Have one reference foo->setfoo(42); if(somecondition) foocounter++; acollection->add(foo); foocounter--; // this pointer not needed if(0 == foocounter) delete foo; This code is messier than we had before But it is beginning to be generic Where do we store foo counter -- especially as we ll need it elsewhere

16 void foo() CSomeObj *obj = new CSomeObject(); bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

17 void foo() count of pointers CSomeObj *obj = new CSomeObject(); bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

18 void foo() count of pointers CSomeObj *obj = new CSomeObject(); bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

19 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 1 bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

20 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 1 bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

21 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 2 bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

22 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 1 bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

23 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 0 bar(obj); obj->dostuff(); void bar(csomeobj *obj) obj->dostuff(); In this example, by the end of foo() we have zero pointers to the object and so we could add code to delete it here

24 void foo() CSomeObj *obj = new CSomeObject(); bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

25 void foo() count of pointers CSomeObj *obj = new CSomeObject(); bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

26 void foo() count of pointers CSomeObj *obj = new CSomeObject(); bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

27 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 1 bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

28 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 1 bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

29 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 2 bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

30 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 3 bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

31 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 2 bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

32 void foo() count of pointers CSomeObj *obj = new CSomeObject(); 1 bar(obj); obj->dostuf(); void bar(csomeobj *obj) someglobal = obj; However, in this example by the end of foo() the global variable someglobal still contains a valid pointer to the object so we cannot delete the object

33 void AMethod(Collection *acollection) Foo *foo = new Foo(); foocounter = 1; // Have one reference foo->setfoo(42); if(somecondition) foocounter++; acollection->add(foo); foocounter--; // this pointer not needed if(0 == foocounter) delete foo; This code is messier than we had before But it is beginning to be generic Where do we store foo counter -- especially as we ll need it elsewhere

34 Collection *acollection = new Collection(); Foo *foo; foo = acollection->getchildat(2); /* Collection has to update foocounter because it copies the pointer foocounter = 2 */ delete acollection; /* foo still valid - foocounter = 1 */ foo->dostuff(); // Wworks foocounter--; // this pointer not needed if(0 == foocounter) delete foo; Collection deletes its children when it is deleted... However foo s foocounter stops the collection deleting it

35 Counting Pointers This works, however it is cumbersome Need integer to store pointer count Where does this go, and how does the code find it? Lots of code repeated everywhere Can we simplify this?

36 Reference counting Use a technique called reference counting Object responsible for counting pointers Call when we copy a pointer Call when finished with pointer Object is responsible for deleting itself Pointers and references mean the same thing

37 class CRCObject public: CRCObject(); void Retain(); /* increase reference count */ void Release(); /* decrease reference count */ private: int m_refcount; Here we have a very simple object, it does nothing but count its own references

38 class CRCObject public: CRCObject(); void Retain(); /* increase reference count */ void Release(); /* decrease reference count */ private: int m_refcount; CRCObject::CRCObject() m_refcount = 1; Set reference count to 1 in the constructor Why, because there is always at least one pointer to the object when it is created!

39 class CRCObject public: CRCObject(); void Retain(); /* increase reference count */ void Release(); /* decrease reference count */ private: int m_refcount; void CRCObject::Retain() ++m_refcount; Retain is very simple we just increase the reference count

40 class CRCObject public: CRCObject(); void Retain(); /* increase reference count */ void Release(); /* decrease reference count */ private: int m_refcount; void CRCObject::Release() if(0 == --m_refcount) delete this; Release is almost as simple. We decrement the reference count and if the new value is zero we delete the current object (as in Java, C++ provides a this variable which points at the current object)

41 CRCObject *rcobj = new CRCObject(); rcobj->retain(); If we didn t set the reference count to one, we would have to use code like this all the time. It s just for convenience...

42 Reference Count With reference counted objects, only the objects delete themselves Client tells Object when it copies the pointer when it has finished with the pointer Object then deletes itself when no longer needed Lets now look over how

43 Shared RC code Reference counting is identical for pretty much all objects Do not need to implement it in each object Can inherit it from a base-class (such as the example CRCObject C++ gotcha: Need to make the destructor virtual

44 class CRCObject public: CRCObject(); virtual ~CRCObject() = 0; /* Destructor */ void Retain(); /* increase reference count */ void Release(); /* decrease reference count */ private: int m_refcount;

45 Using RefCounting Inherit Reference Counting functionality Before you copy a pointer, call Retain() on the when you finish with a pointer, call Release() That s it! Object is deleted when it is appropriate

46 Release() decrements the reference count to zero, and object deletes itself Simple Case void AMethod() Foo *foo = new Foo(); foo->setfoo(42); foo->dostuff(); foo->setfoo(1963); foo->domorestuff(); foo->release(); Assume Foo inherits from CRCobject to get reference counting Reference count is set to one by CRCObject constructor

47 void AMethod(Collection *acollection) Foo *foo = new Foo(); foo->setfoo(42); acollection->add(foo); foo->release(); // Object not destroyed Collection implementation is must call Retain and release -- downside to reference counting Reference count now 2, so when release is called it is decremented to 1 and the object isn t destroyed...

48 Collection *acollection = new Collection(); Foo *foo; AMethod(aCollection); foo = acollection->getchildat(2); // GetChildAt must update reference // count on object it returns acollection->release(); /* foo still valid */ foo->dostuff(); // Will crash eventually Again the collection must be aware that it is handling reference counted objects and call retain in GetChildAt When acollection is released, it calls release on all the objects it contains. This will delete any that are not pointed to by something else so foo is still a valid pointer

49 Reference Counting This is technique is called reference counting Clients never call delete Object is responsible for deleting itself when reference count reaches zero Object needs s to maintain reference count Pointers and references mean the same thing

50 Reference Counting Retain(), called when a copy of the pointer is made and updates the reference count Release(), called before a pointer goes out of scope Release() also contains the check to see if reference count hits zero and if so deletes itself Method names can vary -- e.g. Retain some

51 Reference Counting Very common technique Cocoa development environment for the Mac/iPhone/iPad Microsoft COM Python COM uses AddRef instead of Retain

52 RefCount issues Order matters Can t mix reference counted and nonreferenced counted code easily Easy to miss out calls to Retain() and Release() Circular References!

53 RefCount issues Order of instructions matters Retain() before you copy a pointer If you don t, there is a possibility that the object could go out of scope before you call Retain() Concurrency issues again!

54 CRCObject *q, *p; p->retain(); q = p; CRCObject *q, *p; q = p; q->retain(); Assume a context switch happens at the dotted line. In the left example, the rest of the object p cannot be deleted since we have already increased the reference count In the second, there is a chance that the object is released elsewhere. q then points to a dead block of memory and worse we call s on this dead memory

55 When to retain/release Retain objects when you store the pointer Not always necessary to retain e.g. when passing a pointer to a Scope for the s pointer is covered by scope of the original pointer Method s job to retain object if it keeps pointer

56 CRCObject *p; CRCObject *p; p->retain(); obj->foo(p); p->release(); obj->foo(p); Redundant Retain/Release in example on the left

57 When to retain/release For pointers passed into s If you store the pointer somewhere (e.g within an object), you must retain it Do not release a pointer passed into the

58 All or nothing It s difficult to mix reference counted objects with non-reference counted ones Non-reference counted objects won t update reference count if they copy the pointers Might call delete directly Basically breaks everything! Same is also true the other way, reference counted collections will expect to call retain release an the objects won t accept them

59 Mixing non-rc and RC Vendor provides a class we want to use Doesn t support reference counting Want to use it in a referenced counted environment Could live with it Or adapt it to work reference counted

60 Adapting non-rc code Create a reference counted class with the same s called an Adapter! Wrap non-rc object up inside this objects Pass calls to s on wrapped object Delete object when wrapper deletes itself

61 RC in non-rc environment Again, use an adapter class This time wrap the ref-counted object up And call Release() from the destructor This is known as the Adapter Pattern

62 Circular References Reference Counting is a good, if manual, solution to the lifetime issues In most cases, the objects will be destroyed as soon as they are no longer needed But it is possible to cause objects to not be deleted

63 Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

64 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

65 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

66 1 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

67 1 data data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

68 1 data 1 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

69 data 1 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

70 data 1 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

71 data data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

72 data Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

73 Object references Suppose we have an object, with a pointer to it. the object will have a reference count of 1 This object has a pointer to another object which will also have a reference count of one When the original object s pointer is released, the ref count drops to zero and the object is destroyed. Its destructor will then release its reference to the other object and the result is both objects are destroyed

74 Circular reference case In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference

75 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference data Circular reference case

76 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference data Circular reference case

77 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference 1 data Circular reference case

78 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference 1 data data Circular reference case

79 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference 1 data 1 data Circular reference case

80 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference 1 data 1 data Circular reference case

81 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference 2 data 1 data Circular reference case

82 In this case, the other object also contains a reference back to the first object, so the first object s count is now 2. When the pointer to the original object is released, the reference 1 data 1 data Circular reference case

83 Circular Reference This is called a Circular Reference One object points at another, which points back to the original Could be more than two objects involved Need to break the cycle! Could forbid circular references

84 Strong or weak References One solution is to have two types of references Strong references that updates the reference count Weak references that do not update the reference count

85 strong 1 data 1 data weak

86 Strong or weak Solution works but dangerous What if we have external pointers to both objects Object with strong reference released Second object has reference to dead object E.g.

87 strong 1 data 2 data weak

88 strong 1 2 data weak

89 strong 1 2 data weak

90 strong 1 2 data weak

91 strong 2 data weak

92 2 data weak

93 1 data weak

94 Strong or weak Use when the object with the weak reference is fully contained within the other So you can guarantee the lifetime Otherwise E.g.

95 Reference Count the whole, not the part Circular references common in object hierarchies Looked so far at reference counting single objects Could reference count the whole structure instead

96 Summary Reference Counting provides a simple way to manage the object lifetime Manual Solution Can t mix with non-rc objects easily Circular references Isn t there an automatic solution?

Garbage Collection. Steven R. Bagley

Garbage Collection. Steven R. Bagley Garbage Collection Steven R. Bagley Reference Counting Counts number of pointers to an Object deleted when the count hits zero Eager deleted as soon as it is finished with Problem: Circular references

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 4 Date:

More information

CSE : Python Programming

CSE : Python Programming CSE 399-004: Python Programming Lecture 2: Data, Classes, and Modules January 22, 2007 http://www.seas.upenn.edu/~cse39904/ Administrative things Teaching assistant Brian Summa (bsumma @ seas.upenn.edu)

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers Announcements Please fill out your course evaluations! Those of you interested in becoming an undergraduate mentor for

More information

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

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers Review from Lecture 23 Basic exception mechanisms: try/throw/catch Functions & exceptions, constructors & exceptions Today

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014 Lesson 10A OOP Fundamentals By John B. Owen All rights reserved 2011, revised 2014 Table of Contents Objectives Definition Pointers vs containers Object vs primitives Constructors Methods Object class

More information

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych CS125 : Introduction to Computer Science Lecture Notes #38 and #39 Quicksort c 2005, 2003, 2002, 2000 Jason Zych 1 Lectures 38 and 39 : Quicksort Quicksort is the best sorting algorithm known which is

More information

Weeks 6&7: Procedures and Parameter Passing

Weeks 6&7: Procedures and Parameter Passing CS320 Principles of Programming Languages Weeks 6&7: Procedures and Parameter Passing Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Weeks 6&7: Procedures and Parameter Passing 1 / 45

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

Functions, Pointers, and the Basics of C++ Classes

Functions, Pointers, and the Basics of C++ Classes Functions, Pointers, and the Basics of C++ Classes William E. Skeith III Functions in C++ Vocabulary You should be familiar with all of the following terms already, but if not, you will be after today.

More information

Consider the program...

Consider the program... Smart Pointers Consider the program... When the scope of foo is entered storage for pointer x is created The new allocates storage on the heap class X {... When the scope foo is left, the storage for x

More information

Heap Arrays and Linked Lists. Steven R. Bagley

Heap Arrays and Linked Lists. Steven R. Bagley Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance)

Highlights. - Making threads. - Waiting for threads. - Review (classes, pointers, inheritance) Parallel processing Highlights - Making threads - Waiting for threads - Review (classes, pointers, inheritance) Review: CPUs Review: CPUs In the 2000s, computing too a major turn: multi-core processors

More information

1: Introduction to Object (1)

1: Introduction to Object (1) 1: Introduction to Object (1) 김동원 2003.01.20 Overview (1) The progress of abstraction Smalltalk Class & Object Interface The hidden implementation Reusing the implementation Inheritance: Reusing the interface

More information

Smart Pointers. Some slides from Internet

Smart Pointers. Some slides from Internet Smart Pointers Some slides from Internet 1 Part I: Concept Reference: Using C++11 s Smart Pointers, David Kieras, EECS Department, University of Michigan C++ Primer, Stanley B. Lippman, Jesee Lajoie, Barbara

More information

Functions and Decomposition

Functions and Decomposition Unit 4 Functions and Decomposition Learning Outcomes Design and implement functions to carry out a particular task. Begin to evaluate when it is necessary to split some work into functions. Locate the

More information

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading How C++ Works 1 Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++

More information

Pointers and References. 8-Aug-11

Pointers and References. 8-Aug-11 Pointers and References 8-Aug-11 Machine addresses Computer memory consists of one long list of addressable bytes A pointer is a data item that contains an address 3FA71CF6 A reference is a data item that

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

Memory management COSC346

Memory management COSC346 Memory management COSC346 Life cycle of an object Create a reference pointer Allocate memory for the object Initialise internal data Do stuff Destroy the object Release memory 2 Constructors and destructors

More information

Parallel Programming: Background Information

Parallel Programming: Background Information 1 Parallel Programming: Background Information Mike Bailey mjb@cs.oregonstate.edu parallel.background.pptx Three Reasons to Study Parallel Programming 2 1. Increase performance: do more work in the same

More information

Parallel Programming: Background Information

Parallel Programming: Background Information 1 Parallel Programming: Background Information Mike Bailey mjb@cs.oregonstate.edu parallel.background.pptx Three Reasons to Study Parallel Programming 2 1. Increase performance: do more work in the same

More information

Lecture Notes on Garbage Collection

Lecture Notes on Garbage Collection Lecture Notes on Garbage Collection 15-411: Compiler Design André Platzer Lecture 20 1 Introduction In the previous lectures we have considered a programming language C0 with pointers and memory and array

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

Procedures, Parameters, Values and Variables. Steven R. Bagley

Procedures, Parameters, Values and Variables. Steven R. Bagley Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

More information

Garbage Collection. CS 351: Systems Programming Michael Saelee

Garbage Collection. CS 351: Systems Programming Michael Saelee Garbage Collection CS 351: Systems Programming Michael Saelee = automatic deallocation i.e., malloc, but no free! system must track status of allocated blocks free (and potentially reuse)

More information

Software Engineering /48

Software Engineering /48 Software Engineering 1 /48 Topics 1. The Compilation Process and You 2. Polymorphism and Composition 3. Small Functions 4. Comments 2 /48 The Compilation Process and You 3 / 48 1. Intro - How do you turn

More information

Memory Management: The Details

Memory Management: The Details Lecture 10 Memory Management: The Details Sizing Up Memory Primitive Data Types Complex Data Types byte: char: short: basic value (8 bits) 1 byte 2 bytes Pointer: platform dependent 4 bytes on 32 bit machine

More information

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() { Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative

More information

G52CPP C++ Programming Lecture 20

G52CPP C++ Programming Lecture 20 G52CPP C++ Programming Lecture 20 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Wrapping up Slicing Problem Smart pointers More C++ things Exams 2 The slicing problem 3 Objects are not

More information

Generics/Templates. Steven R. Bagley

Generics/Templates. Steven R. Bagley Generics/Templates Steven R. Bagley Code reuse Often we implement a class Only to find that next week, we need to reuse it with just a slight tweak Collections, Containers are good examples class CIntStack

More information

Ch. 11: References & the Copy-Constructor. - continued -

Ch. 11: References & the Copy-Constructor. - continued - Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

Financial computing with C++

Financial computing with C++ Financial Computing with C++, Lecture 6 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 6 - p2/24 Outline Linked lists Linked

More information

Session 4 Starting the Air Raid Game

Session 4 Starting the Air Raid Game Session 4 Starting the Air Raid Game Authored by Brian Cullen (bcullen@rossettschool.co.uk/@mrbcullen) (c) Copyright 2011 Computing At School. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike

More information

Advanced Computer Programming

Advanced Computer Programming Hazırlayan Yard. Doç. Dr. Mehmet Fidan Functions are integrities of variables and expressions that are used to fulfill a job. They take definite parameter types and have definite return types. They can

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting Computer Science 5 Problem Solving with Java The College of Saint Rose Spring 016 Topic Notes: Searching and Sorting Searching We all know what searching is looking for something. In a computer program,

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Construction: High quality code for programming in the large

Construction: High quality code for programming in the large Construction: High quality code for programming in the large Paul Jackson School of Informatics University of Edinburgh What is high quality code? High quality code does what it is supposed to do......

More information

Lab 4. Out: Friday, February 25th, 2005

Lab 4. Out: Friday, February 25th, 2005 CS034 Intro to Systems Programming Doeppner & Van Hentenryck Lab 4 Out: Friday, February 25th, 2005 What you ll learn. In this lab, you ll learn to use function pointers in a variety of applications. You

More information

Moving from FrameMaker to Blaze: Best Practices

Moving from FrameMaker to Blaze: Best Practices Moving from Adobe FrameMaker to MadCap Blaze is easy, although to get the best results you need to do some planning before you start. This document discusses suggestions and issues to make the import result

More information

Programming Language Implementation

Programming Language Implementation A Practical Introduction to Programming Language Implementation 2014: Week 10 Garbage Collection College of Information Science and Engineering Ritsumeikan University 1 review of last week s topics dynamic

More information

Annotation Hammer Venkat Subramaniam (Also published at

Annotation Hammer Venkat Subramaniam (Also published at Annotation Hammer Venkat Subramaniam venkats@agiledeveloper.com (Also published at http://www.infoq.com) Abstract Annotations in Java 5 provide a very powerful metadata mechanism. Yet, like anything else,

More information

CS61B Lecture #7. Announcements:

CS61B Lecture #7. Announcements: Announcements: CS61B Lecture #7 New discussion section: Tuesday 2 3PM in 310 Soda. New lab section: Thursday 2 4PM in 273 Soda. Programming Contest coming up: 5 October (new date). Watch for details. Last

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

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

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory Outline 1 Chapter 10: C++ Dynamic Memory Proper Memory Management Classes which must allocate memory must manage it properly. Default behavior of operations in C++ are insufficient for this. The assignment

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Pointers and References

Pointers and References Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers

More information

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS 164 Spring 2005 P. N. Hilfinger Project #2: Static Analyzer for Pyth Due: Wednesday, 6 April

More information

Analyzing Systems. Steven M. Bellovin November 26,

Analyzing Systems. Steven M. Bellovin November 26, Analyzing Systems When presented with a system, how do you know it s secure? Often, you re called upon to analyze a system you didn t design application architects and programmers build it; security people

More information

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case) Design Phase Create a class Person Determine the superclass NSObject (in this case) 8 Design Phase Create a class Person Determine the superclass NSObject (in this case) What properties should it have?

More information

Mobile Application Development

Mobile Application Development Mobile Application Development Lecture 13 Introduction to ObjectiveC Part II 2013/2014 Parma Università degli Studi di Parma Lecture Summary Object creation Memory management Automatic Reference Counting

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

C# Language. CSE 409 Advanced Internet Technology

C# Language. CSE 409 Advanced Internet Technology C# Language Today You will learn Building a basic class Value Types and Reference Types Understanding Namespaces and Assemblies Advanced Class Programming CSE 409 Advanced Internet Technology Building

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

Mobile Application Programming. Objective-C Classes

Mobile Application Programming. Objective-C Classes Mobile Application Programming Objective-C Classes Custom Classes @interface Car : NSObject #import Car.h + (int) viper; - (id) initwithmodel:(int)m; @implementation Car Point position; float velocity;

More information

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable CISC-124 20180122 Today we looked at casting, conditionals and loops. Casting Casting is a simple method for converting one type of number to another, when the original type cannot be simply assigned to

More information

CS 241 Honors Memory

CS 241 Honors Memory CS 241 Honors Memory Ben Kurtovic Atul Sandur Bhuvan Venkatesh Brian Zhou Kevin Hong University of Illinois Urbana Champaign February 20, 2018 CS 241 Course Staff (UIUC) Memory February 20, 2018 1 / 35

More information

int $0x32 // call interrupt number 50

int $0x32 // call interrupt number 50 Kernel Programming: Process isolation, goal to make programs run fast and reliably o Processes should not affect others, unless there s a specific and allowed communication channel o Each process can act

More information

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties CS193P - Lecture 3 iphone Application Development Custom Classes Object Lifecycle Autorelease Properties 1 Announcements Assignments 1A and 1B due Wednesday 1/13 at 11:59 PM Enrolled Stanford students

More information

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table

More information

Structure of Programming Languages Lecture 10

Structure of Programming Languages Lecture 10 Structure of Programming Languages Lecture 10 CS 6636 4536 Spring 2017 CS 6636 4536 Lecture 10: Classes... 1/23 Spring 2017 1 / 23 Outline 1 1. Types Type Coercion and Conversion Type Classes, Generics,

More information

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP Critique this Code string const & findsmallest(vector const &v) string smallest = v[0]; for (unsigned int i = 0; i < v.size(); i++) if (v[i] < smallest) smallest = v[i]; return smallest; Hint:

More information

Laboratorio di Tecnologie dell'informazione

Laboratorio di Tecnologie dell'informazione Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Exceptions What are exceptions? Exceptions are a mechanism for handling an error

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008 Dynamic Storage Allocation CS 44: Operating Systems Spring 2 Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need to grow or shrink. Dynamic

More information

MEMORY MANAGEMENT IN C++ AND JAVA

MEMORY MANAGEMENT IN C++ AND JAVA MEMORY MANAGEMENT IN C++ AND JAVA Gayathri Kandasamy Sengottaiyan gayathri.sengottain@gmail.com Professor: Tarik El Taeib teltaeib@my.bridgeport.edu University Of Bridgeport -Computer Science ABSTRACT

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: Linked Lists Object References When you declare a variable of a non-primitive type you are really declaring a reference to that object String

More information

CS64 Week 5 Lecture 1. Kyle Dewey

CS64 Week 5 Lecture 1. Kyle Dewey CS64 Week 5 Lecture 1 Kyle Dewey Overview More branches in MIPS Memory in MIPS MIPS Calling Convention More Branches in MIPS else_if.asm nested_if.asm nested_else_if.asm Memory in MIPS Accessing Memory

More information

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language Categories of languages that support OOP: 1. OOP support is added to an existing language - C++ (also supports procedural and dataoriented programming) - Ada 95 (also supports procedural and dataoriented

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.. COMP6771 Advanced C++ Programming Week 5 Part Two: Dynamic Memory Management 2016 www.cse.unsw.edu.au/ cs6771 2.. Revisited 1 #include 2 3 struct X { 4 X() { std::cout

More information

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

QUIZ How do we implement run-time constants and. compile-time constants inside classes? QUIZ How do we implement run-time constants and compile-time constants inside classes? Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

Static, Final & Memory Management

Static, Final & Memory Management Static, Final & Memory Management The static keyword What if you want to have only one piece of storage regardless of how many objects are created or even no objects are created? What if you need a method

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

The University of Nottingham

The University of Nottingham The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER OBJECT-ORIENTED METHODS Time allowed TWO hours Candidates may complete the front cover of their answer book and

More information

Questions: ECE551 PRACTICE Final

Questions: ECE551 PRACTICE Final ECE551 PRACTICE Final This is a full length practice midterm exam. If you want to take it at exam pace, give yourself 175 minutes to take the entire test. Just like the real exam, each question has a point

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova CBook a = CBook("C++", 2014); CBook b = CBook("Physics", 1960); a.display(); b.display(); void CBook::Display() cout

More information

XP: Backup Your Important Files for Safety

XP: Backup Your Important Files for Safety XP: Backup Your Important Files for Safety X 380 / 1 Protect Your Personal Files Against Accidental Loss with XP s Backup Wizard Your computer contains a great many important files, but when it comes to

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli COMS W3101 Programming Language: C++ (Fall 2015) ramana@cs.columbia.edu Lecture-2 Overview of C continued C character arrays Functions Structures Pointers C++ string class C++ Design, difference with C

More information

Heap Arrays. Steven R. Bagley

Heap Arrays. Steven R. Bagley Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,

More information

CmpSci 187: Programming with Data Structures Spring 2015

CmpSci 187: Programming with Data Structures Spring 2015 CmpSci 187: Programming with Data Structures Spring 2015 Lecture #13, Concurrency, Interference, and Synchronization John Ridgway March 12, 2015 Concurrency and Threads Computers are capable of doing more

More information

1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting

1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting 1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, 1996 2 Bucket Sorting We ve seen various algorithms for sorting in O(n log n) time and a lower bound showing that O(n log n) is

More information

ITP 342 Advanced Mobile App Dev. Memory

ITP 342 Advanced Mobile App Dev. Memory ITP 342 Advanced Mobile App Dev Memory Memory Management Objective-C provides two methods of application memory management. 1. In the method described in this guide, referred to as manual retain-release

More information

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson Copyie Elesion from the C++11 mass 9 Sep 2016 Pete Williamson C++ 11 is a whole new language Links to learning more: http://en.cppreference.com/w/cpp/language/copy_elision https://engdoc.corp.google.com/eng/doc/devguide/cpp/cpp11.shtml?cl=head

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

CSI33 Data Structures

CSI33 Data Structures 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

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

Memory Management Techniques

Memory Management Techniques Memory Management Techniques How can we ensure we clean up after ourselves? 1 You Have Two Choices Dynamically allocated objects are owned by some other object who is responsible for deleting them Dynamically

More information

Intro to Algorithms. Professor Kevin Gold

Intro to Algorithms. Professor Kevin Gold Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in

More information