forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
294 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// 5.1.shared.ptr.cpp | ||
// chapter 05 start pointers and memory management | ||
// modern c++ tutorial | ||
// | ||
// created by changkun at changkun.de | ||
// https://github.com/changkun/modern-cpp-tutorial | ||
// | ||
|
||
#include <iostream> | ||
#include <memory> | ||
|
||
void foo(std::shared_ptr<int> i) | ||
{ | ||
(*i)++; | ||
} | ||
|
||
int main() | ||
{ | ||
// auto pointer = new int(10); // illegal, no direct assignment | ||
// std::shared_ptr construction | ||
auto pointer = std::make_shared<int>(10); | ||
auto pointer2 = pointer; // reference count + 1 | ||
auto pointer3 = pointer; // reference count + 1 | ||
|
||
|
||
foo(pointer); | ||
std::cout << *pointer << std::endl; // 11 | ||
int *p = pointer.get(); // does not increase reference count | ||
|
||
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; | ||
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; | ||
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; | ||
|
||
pointer2.reset(); | ||
std::cout << "reset pointer2:" << std::endl; | ||
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; | ||
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; | ||
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; | ||
|
||
pointer3.reset(); | ||
std::cout << "reset pointer3:" << std::endl; | ||
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; | ||
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; | ||
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; | ||
// std::cout << *pointer << std::endl; // reference count equals 0, illegal access | ||
|
||
|
||
// Before leaving the scope, the pointer is destructed and | ||
// the reference count is reduced to 0 | ||
return 0; | ||
} |
Oops, something went wrong.