mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
@@ -1,66 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
void foo(std::shared_ptr<int> i)
|
||||
{
|
||||
(*i)++;
|
||||
}
|
||||
|
||||
|
||||
//struct Base {
|
||||
// Base() { std::cout << " Base::Base()\n"; }
|
||||
// ~Base() { std::cout << " Base::~Base()\n"; }
|
||||
//};
|
||||
//
|
||||
//struct Subclass: public Base {
|
||||
// Subclass() { std::cout << " Subclass::Subclass()\n"; }
|
||||
// ~Subclass() { std::cout << " Subclass::~Subclass()\n"; }
|
||||
//};
|
||||
//
|
||||
//void thr(std::shared_ptr<Base> p)
|
||||
//{
|
||||
// std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
// std::shared_ptr<Base> lp = p; // 即使引用计数增加也是线程安全的
|
||||
// {
|
||||
// static std::mutex io_mutex;
|
||||
// std::lock_guard<std::mutex> lk(io_mutex);
|
||||
// std::cout << "local pointer in a thread:\n"
|
||||
// << " lp.get() = " << lp.get()
|
||||
// << ", lp.use_count() = " << lp.use_count() << '\n';
|
||||
// }
|
||||
//}
|
||||
|
||||
int main()
|
||||
{
|
||||
// auto pointer = new int(10); // 非法, 不允许直接赋值
|
||||
// 构造了一个 std::shared_ptr
|
||||
auto pointer = std::make_shared<int>(10);
|
||||
auto pointer2 = pointer; // 引用计数+1
|
||||
auto pointer3 = pointer; // 引用计数+1
|
||||
|
||||
|
||||
foo(pointer);
|
||||
std::cout << *pointer << std::endl; // 11
|
||||
int *p = pointer.get(); // 这样不会增加引用计数
|
||||
|
||||
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; // 引用计数为0时, 非法访问
|
||||
|
||||
|
||||
// 离开作用域前,pointer 会被析构,引用计数减为0, 从而释放内存
|
||||
return 0;
|
||||
}
|
||||
52
code/5/5.1.shared.ptr.a.cpp
Normal file
52
code/5/5.1.shared.ptr.a.cpp
Normal file
@@ -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;
|
||||
}
|
||||
@@ -1,3 +1,12 @@
|
||||
//
|
||||
// 5.2.unique.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>
|
||||
|
||||
@@ -14,28 +23,28 @@ void f(const Foo &) {
|
||||
int main() {
|
||||
std::unique_ptr<Foo> p1(std::make_unique<Foo>());
|
||||
|
||||
// p1 不空, 输出
|
||||
// p1 is not empty, prints
|
||||
if (p1) p1->foo();
|
||||
{
|
||||
std::unique_ptr<Foo> p2(std::move(p1));
|
||||
|
||||
// p2 不空, 输出
|
||||
// p2 is not empty, prints
|
||||
f(*p2);
|
||||
|
||||
// p2 不空, 输出
|
||||
// p2 is not empty, prints
|
||||
if(p2) p2->foo();
|
||||
|
||||
// p1 为空, 无输出
|
||||
// p1 is empty, no prints
|
||||
if(p1) p1->foo();
|
||||
|
||||
p1 = std::move(p2);
|
||||
|
||||
// p2 为空, 无输出
|
||||
// p2 is empty, no prints
|
||||
if(p2) p2->foo();
|
||||
std::cout << "p2 被销毁" << std::endl;
|
||||
std::cout << "p2 was destroied" << std::endl;
|
||||
}
|
||||
// p1 不空, 输出
|
||||
// p1 is not empty, prints
|
||||
if (p1) p1->foo();
|
||||
|
||||
// Foo 的实例会在离开作用域时被销毁
|
||||
// Foo instance will be destroied when leaving the scope
|
||||
}
|
||||
@@ -1,3 +1,13 @@
|
||||
//
|
||||
// 5.3.weak.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>
|
||||
|
||||
@@ -8,14 +18,14 @@ class A {
|
||||
public:
|
||||
std::shared_ptr<B> pointer;
|
||||
~A() {
|
||||
std::cout << "A 被销毁" << std::endl;
|
||||
std::cout << "A was destroied" << std::endl;
|
||||
}
|
||||
};
|
||||
class B {
|
||||
public:
|
||||
std::shared_ptr<A> pointer;
|
||||
~B() {
|
||||
std::cout << "B 被销毁" << std::endl;
|
||||
std::cout << "B was destroied" << std::endl;
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
14
code/5/Makefile
Normal file
14
code/5/Makefile
Normal file
@@ -0,0 +1,14 @@
|
||||
#
|
||||
# modern cpp tutorial
|
||||
#
|
||||
# created by changkun at changkun.de
|
||||
# https://github.com/changkun/modern-cpp-tutorial
|
||||
#
|
||||
|
||||
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
|
||||
|
||||
%.out: %.cpp Makefile
|
||||
clang++ $< -o $@ -std=c++2a -pedantic
|
||||
|
||||
clean:
|
||||
rm *.out
|
||||
Reference in New Issue
Block a user