![Page 2: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/2.jpg)
Recap
Conversion Operators
RAII
Smart Pointers
Game Plan
![Page 3: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/3.jpg)
Recap
![Page 4: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/4.jpg)
Initialisation:
Transforms an object’s initial junk data into valid data.
Assignment:
Replaces existing valid data with other valid data.
Initialisation vs Assignment
![Page 5: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/5.jpg)
Constructors
Normal Constructor:
● What you are used to!
Copy Constructor
● Initialise an instance of a type to be a copy of another instance
Copy Assignment
● Not a constructor
● Assign an instance of a type to be a copy of another instance
![Page 6: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/6.jpg)
The Rule of Three
If you implement a copy constructor, assignment operator, or destructor, you should implement the others, as well
![Page 7: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/7.jpg)
Conversion Operators
![Page 8: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/8.jpg)
Conversion Operators
Let you define how a class can be converted to other types.
For example, we could define a conversion of the MyVector to a bool to be false if the vector is empty and true otherwise.
![Page 9: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/9.jpg)
Converting to Type works by overloading the Type() operator.
Doesn’t have a return value.
class MyClass {
public:operator Type() {
// return something of type Type}
}
Conversion Operators
![Page 10: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/10.jpg)
An example defining a bool conversion for MyVector :
class MyVector {public:
operator bool() {return empty();
}};
MyVector v;if(v) {
cout << v[0] << endl;}
Conversion Operators
![Page 11: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/11.jpg)
RAII
![Page 12: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/12.jpg)
Resource
Let’s first talk about the abstraction of a resource.
We will look at file opening and closing in C as a case study.
![Page 13: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/13.jpg)
C File I/O
To read a file in C, you need to:
1. Open the file with fopen2. Read data using fgets3. Close the open file with fclose
If a programmer doesn’t remember to close an open file, bad things happen (memory leaks, crashes etc.)
![Page 14: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/14.jpg)
C File I/O
You can think of this as a resource.
What is a resource?
● Anything that exists in limited supply.● Something you have to acquire and release.
Examples: memory, open files, sockets etc.
![Page 15: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/15.jpg)
C File I/O
To read a file in C, you need to:
1. Open the file with fopen2. Read data using fgets3. Close the open file with fclose
If a programmer doesn’t remember to close an open file, bad things happen (memory leaks, crashes etc.)
![Page 16: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/16.jpg)
C File I/O
To read a file in C, you need to:
1. Open the file with fopen // acquire2. Read data using fgets3. Close the open file with fclose
If a programmer doesn’t remember to close an open file, bad things happen (memory leaks, crashes etc.)
![Page 17: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/17.jpg)
C File I/O
To read a file in C, you need to:
1. Open the file with fopen // acquire2. Read data using fgets 3. Close the open file with fclose // release
If a programmer doesn’t remember to close an open file, bad things happen (memory leaks, crashes etc.)
![Page 18: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/18.jpg)
Resources
Other examples of resources:
Acquire Release
Files fopen fclose
Memory new, new[] delete, delete[]
Locks lock, try_lock unlock
Sockets socket close
![Page 19: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/19.jpg)
RAIIResource Acquisition Is Initialisation
![Page 20: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/20.jpg)
RAII
A modern C++ idiom.
When you initialize an object, it should already have acquired any resources it needs (in the constructor).
When an object goes out of scope, it should release every resource it is using (using the destructor).
![Page 21: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/21.jpg)
Key points:
● There should never be a half-ready or half-dead object.
● When an object is created, it should be in a ready state.
● When an object goes out of scope, it should release its resources.
RAII
![Page 22: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/22.jpg)
Key points:
● There should never be a half-ready or half-dead object.
● When an object is created, it should be in a ready state.
● When an object goes out of scope, it should release its resources.
RAII
The user shouldn’t have to do anything more.
![Page 23: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/23.jpg)
C File I/OHow does C File I/O violate RAII?
void printFile(const char* name) {// acquire file resourceFILE* f = fopen(name, "r");
// print contents of f
// release file resourcefclose(f);
}
![Page 24: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/24.jpg)
C File I/OHow does C File I/O violate RAII?
void printFile(const char* name) {// acquire file resourceFILE* f = fopen(name, "r");
// print contents of f
// release file resourcefclose(f);
}
![Page 25: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/25.jpg)
C File I/OHow does C File I/O violate RAII?
void printFile(const char* name) {// acquire file resourceFILE* f = fopen(name, "r");
// print contents of f
// release file resource?
}
![Page 26: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/26.jpg)
C File I/OHow does C File I/O violate RAII?
void printFile(const char* name) {// acquire file resourceFILE* f = fopen(name, "r");
// print contents of f
// release file resource?
}
f goes out of scope, but doesn’t release its resources.
![Page 27: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/27.jpg)
C File I/O
What would be an RAII friendly solution for C File I/O?
![Page 28: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/28.jpg)
class FileObj {public:
FILE* ptr;FileObj(char* name)
: ptr(fopen(name, "r") {}
~FileObj() {fclose(ptr);
}};
C File I/O + RAII
![Page 29: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/29.jpg)
C File I/OOur new printFile method would look like:
void printFile(const char* name) {// initialization will acquire resourcesFileObj fobj(name);
// print contents of f
// FileObj destructor will release resources}
![Page 30: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/30.jpg)
In fact, you have already been using RAII!
For example:
● You can create an ifstream and it will open the file
● When the ifstream goes out of scope, its destructor closes the file.
Don’t actually need to call the .close() method.
C File I/O
![Page 31: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/31.jpg)
RAII - An Aside
RAII is a bad name for the concept.
"The best example of why I shouldn't be in marketing"
"I didn't have a good day when I named that"
Bjarne Stroustrup, still unhappy with the name RAII in 2012
![Page 32: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/32.jpg)
RAII - An Aside
A better name is probably:
Constructor Acquires, Destructor Releases
or
Scope Based Resource Management
![Page 33: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/33.jpg)
Smart Pointers
![Page 34: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/34.jpg)
Smart Pointers
What is another thing that violates RAII?
Raw Pointers and heap allocation!
![Page 35: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/35.jpg)
Smart Pointers
Calls to new acquire resource (memory).
Calls to delete release resource.
But this is not automatically done when the pointers go out
of scope.
![Page 36: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/36.jpg)
Smart Pointers
But this is not automatically done when the pointers go out of scope:
void rawPtrFn() {// acquire memory resourceNode* n = new Node;
// manually release memorydelete n;
}
![Page 37: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/37.jpg)
Smart Pointers
But this is not automatically done when the pointers go out of scope:
void rawPtrFn() {// acquire memory resourceNode* n = new Node;
// manually release memorydelete n;
}If we forget this, we leak memory.
![Page 38: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/38.jpg)
Smart Pointers
What would be an RAII solution to this?
Have a class that
● Allocates the memory when initialized
● Frees the memory when destructor is called
● Allows access to underlying pointer
![Page 39: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/39.jpg)
Smart Pointers
Let’s plan and write this up:
Smart Pointers(RAIIPtr_unique.pro)
![Page 40: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/40.jpg)
First we make a smart pointer
Smart Pointers
Data (heap)resource
SmartPtr<int> x;
![Page 41: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/41.jpg)
We then make a copy of our smart pointer
Smart Pointers
Data (heap)resource
SmartPtr<int> x;
resource
SmartPtr<int> y;
![Page 42: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/42.jpg)
When y goes out of scope, it deletes the heap data
Smart Pointers
Data (heap)resource
SmartPtr<int> x;
resource
SmartPtr<int> y;
![Page 43: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/43.jpg)
This leaves x pointing at deallocated data
Smart Pointers
resource
SmartPtr<int> x;
![Page 44: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/44.jpg)
If we dereference x or its destructor calls delete, we crash
Smart Pointers
resource
SmartPtr<int> x;
![Page 45: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/45.jpg)
If we dereference x or its destructor calls delete, we crash
Smart Pointers
resource
SmartPtr<int> x;
��
![Page 46: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/46.jpg)
Smart Pointers
Have to be careful when copying an RAII object
Don’t want two objects thinking they both exclusively own a resource
![Page 47: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/47.jpg)
C++ already has built-in smart pointers.
● std::unique_ptr
● std::shared_ptr
● std::weak_ptr
Smart Pointers
![Page 48: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/48.jpg)
Similar to what we wrote earlier
Uniquely own its resource and deletes it when the object is destroyed
Cannot be copied!
{
std::unique_ptr<int> p(new int);
// Use p
}
// Freed!
unique_ptr
![Page 49: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/49.jpg)
Resource can be stored by any number of shared_ptrs
Deleted when none of them point to it
{
std::shared_ptr<int> p1(new int);
// Use p1
{
std::shared_ptr<int> p2 = p1;
// Use p1 and p2
}
// Use p1
}
// Freed!
shared_ptr
![Page 50: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/50.jpg)
How are these implemented?
Reference counting!
Store an int that keeps track of the number currently referencing that data
Gets incremented in copy constructor/copy assignment
Gets decremented in destructor or when overwritten with copy assignment
Frees the resource when reference count hits 0
shared_ptr
![Page 51: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/51.jpg)
Smart Pointers
See Course Reader pg. 351 onwards for details. Let’s plan
and write this up:
Smart Pointers(RAIIPtr_shared.pro)
![Page 52: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/52.jpg)
Used to deal with circular references of shared_ptr
Read documentation to learn more!
weak_ptr
![Page 53: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/53.jpg)
Next TimeFinal Topics
![Page 54: RAII and Smart Pointers - Stanford University...When an object is created, it should be in a ready state. When an object goes out of scope, it should release its resources. RAII](https://reader033.vdocument.in/reader033/viewer/2022060906/60a1073e2f0c742be619f46c/html5/thumbnails/54.jpg)