mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-16 20:27:08 +03:00
book: typo and grammar fixes (#185)
This commit is contained in:
@@ -18,11 +18,11 @@ In traditional C++, "remembering" to manually release resources is not always a
|
||||
So the usual practice is that for an object, we apply for space when constructor, and free space when the destructor (called when leaving the scope).
|
||||
That is, we often say that the RAII resource acquisition is the initialization technology.
|
||||
|
||||
There are exceptions to everything, we always have the need to allocate objects on free storage. In traditional C++ we have to use `new` and `delete` to "remember" to release resources. C++11 introduces the concept of smart pointers, using the idea of reference counting, so that programmers no longer need to care about manually releasing memory.
|
||||
There are exceptions to everything, we always need to allocate objects on free storage. In traditional C++ we have to use `new` and `delete` to "remember" to release resources. C++11 introduces the concept of smart pointers, using the idea of reference counting so that programmers no longer need to care about manually releasing memory.
|
||||
These smart pointers include `std::shared_ptr`/`std::unique_ptr`/`std::weak_ptr`, which need to include the header file `<memory>`.
|
||||
|
||||
> Note: The reference count is not garbage collection. The reference count can recover the objects that are no longer used as soon as possible, and will not cause long waits during the recycling process.
|
||||
> More clearly and clearly indicate the life cycle of resources.
|
||||
> More clearly and indicate the life cycle of resources.
|
||||
|
||||
## 5.2 `std::shared_ptr`
|
||||
|
||||
@@ -86,7 +86,7 @@ std::unique_ptr<int> pointer2 = pointer; // illegal
|
||||
```
|
||||
|
||||
> `make_unique` is not complicated. C++11 does not provide `std::make_unique`, which can be implemented by itself:
|
||||
>
|
||||
>
|
||||
> ```cpp
|
||||
> template<typename T, typename ...Args>
|
||||
> std::unique_ptr<T> make_unique( Args&& ...args ) {
|
||||
@@ -114,30 +114,30 @@ void f(const Foo &) {
|
||||
|
||||
int main() {
|
||||
std::unique_ptr<Foo> p1(std::make_unique<Foo>());
|
||||
|
||||
|
||||
// p1 is not empty, prints
|
||||
if (p1) p1->foo();
|
||||
{
|
||||
std::unique_ptr<Foo> p2(std::move(p1));
|
||||
|
||||
|
||||
// p2 is not empty, prints
|
||||
f(*p2);
|
||||
|
||||
|
||||
// p2 is not empty, prints
|
||||
if(p2) p2->foo();
|
||||
|
||||
|
||||
// p1 is empty, no prints
|
||||
if(p1) p1->foo();
|
||||
|
||||
|
||||
p1 = std::move(p2);
|
||||
|
||||
|
||||
// p2 is empty, no prints
|
||||
if(p2) p2->foo();
|
||||
std::cout << "p2 was destroyed" << std::endl;
|
||||
}
|
||||
// p1 is not empty, prints
|
||||
if (p1) p1->foo();
|
||||
|
||||
|
||||
// Foo instance will be destroyed when leaving the scope
|
||||
}
|
||||
```
|
||||
@@ -172,12 +172,12 @@ int main() {
|
||||
std::shared_ptr<B> b = std::make_shared<B>();
|
||||
a->pointer = b;
|
||||
b->pointer = a;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
The result is that A and B will not be destroyed. This is because the pointer inside a, b also references `a, b`, which makes the reference count of `a, b` become 2, leaving the scope. When the `a, b` smart pointer is destructed, it can only cause the reference count of this area to be decremented by one. This causes the memory area reference count pointed to by the `a, b` object to be non-zero, but the external has no way to find this area, it also caused a memory leak, as shown in Figure 5.1:
|
||||
The result is that A and B will not be destroyed. This is because the pointer inside a, b also references `a, b`, which makes the reference count of `a, b` becomes 2, leaving the scope. When the `a, b` smart pointer is destructed, it can only cause the reference count of this area to be decremented by one. This causes the memory area reference count pointed to by the `a, b` object to be non-zero, but the external has no way to find this area, it also caused a memory leak, as shown in Figure 5.1:
|
||||
|
||||

|
||||
|
||||
@@ -187,7 +187,7 @@ The solution to this problem is to use the weak reference pointer `std::weak_ptr
|
||||
|
||||
In the above figure, only B is left in the last step, and B does not have any smart pointers to reference it, so this memory resource will also be released.
|
||||
|
||||
`std::weak_ptr` has no `*` operator and `->` operator, so it can't operate on resources. Its only function is to check if `std::shared_ptr` exists, its `expired()` method can return `false` when the resource is not released, otherwise it returns `true`.
|
||||
`std::weak_ptr` has no `*` operator and `->` operator, so it can't operate on resources. Its only function is to check if `std::shared_ptr` exists, its `expired()` method can return `false` when the resource is not released, otherwise, it returns `true`.
|
||||
|
||||
## Conclusion
|
||||
|
||||
|
||||
Reference in New Issue
Block a user