adding shared pointer example
This commit is contained in:
committed by
JamesFlare1212
parent
30fa11776d
commit
329699721f
@@ -325,3 +325,33 @@ int main() {
|
|||||||
- std::weak_ptr: Use with shared_ptr. Memory is destroyed when no more shared_ptrs are pointing to object. So each time a weak_ptr is used you should first “lock” the data by creating a shared_ptr.
|
- std::weak_ptr: Use with shared_ptr. Memory is destroyed when no more shared_ptrs are pointing to object. So each time a weak_ptr is used you should first “lock” the data by creating a shared_ptr.
|
||||||
|
|
||||||
- std::scoped_ptr: (Boost) “Remembers” to delete things when they go out of scope. Alternate to auto_ptr. Cannot be copied.
|
- std::scoped_ptr: (Boost) “Remembers” to delete things when they go out of scope. Alternate to auto_ptr. Cannot be copied.
|
||||||
|
|
||||||
|
## 27.15 Exercise
|
||||||
|
|
||||||
|
In the following program, the use count will be printed 3 times. What exact value will be printed each time?
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::shared_ptr<int> age(new int(40));
|
||||||
|
std::cout << "age is " << *age << std::endl;
|
||||||
|
|
||||||
|
// you can never do this, which is assigning a smarter pointer to a raw pointer.
|
||||||
|
// int * temp = age;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::shared_ptr<int> temp = age;
|
||||||
|
std::cout << "age is " << *temp << std::endl;
|
||||||
|
std::cout << "the use count is : " << age.use_count() << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "the use count is : " << age.use_count() << std::endl;
|
||||||
|
|
||||||
|
// give up my ownership, it decreases the reference count of the managed object by one.
|
||||||
|
// if that shared pointer was the last owner (i.e., reference count becomes zero), the object is deleted.
|
||||||
|
// the shared_ptr itself is now empty (i.e., it holds nullptr).
|
||||||
|
age.reset();
|
||||||
|
std::cout << "the use count is : " << age.use_count() << std::endl;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
23
lectures/27_garbage_collection/shared_ptr1.cpp
Normal file
23
lectures/27_garbage_collection/shared_ptr1.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::shared_ptr<int> age(new int(40));
|
||||||
|
std::cout << "age is " << *age << std::endl;
|
||||||
|
|
||||||
|
// you can never do this, which is assigning a smarter pointer to a raw pointer.
|
||||||
|
// int * temp = age;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::shared_ptr<int> temp = age;
|
||||||
|
std::cout << "age is " << *temp << std::endl;
|
||||||
|
std::cout << "the use count is : " << age.use_count() << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "the use count is : " << age.use_count() << std::endl;
|
||||||
|
|
||||||
|
// give up my ownership, it decreases the reference count of the managed object by one.
|
||||||
|
// if that shared pointer was the last owner (i.e., reference count becomes zero), the object is deleted.
|
||||||
|
// the shared_ptr itself is now empty (i.e., it holds nullptr).
|
||||||
|
age.reset();
|
||||||
|
std::cout << "the use count is : " << age.use_count() << std::endl;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user