line breaks

This commit is contained in:
Jidong Xiao
2023-11-25 00:08:54 -05:00
parent d216c13d6d
commit 495edc8e6a

View File

@@ -22,6 +22,8 @@ have performance and memory usage disadvantages.
- For our discussion today, well assume that all program data is stored in dynamically-allocated instances of the - For our discussion today, well assume that all program data is stored in dynamically-allocated instances of the
following simple class. This class can be used to build linked lists, trees, and graphs with cycles: following simple class. This class can be used to build linked lists, trees, and graphs with cycles:
```cpp
class Node { class Node {
public: public:
Node(char v, Node* l, Node* r) : Node(char v, Node* l, Node* r) :
@@ -30,6 +32,7 @@ public:
Node* left; Node* left;
Node* right; Node* right;
}; };
```
## 25.3 Garbage Collection Technique #1: Reference Counting ## 25.3 Garbage Collection Technique #1: Reference Counting
@@ -143,20 +146,20 @@ stack:
## 25.10 Garbage Collection Comparison ## 25.10 Garbage Collection Comparison
- Reference Counting: - Reference Counting:
+ fast and incremental + fast and incremental
cant handle cyclical data structures! cant handle cyclical data structures!
? requires 33% extra memory (1 integer per node) ? requires 33% extra memory (1 integer per node)
- Stop & Copy: - Stop & Copy:
requires a long pause in program execution requires a long pause in program execution
+ can handle cyclical data structures! + can handle cyclical data structures!
requires 100% extra memory (you can only use half the memory) requires 100% extra memory (you can only use half the memory)
+ runs fast if most of the memory is garbage (it only touches the nodes reachable from the root) + runs fast if most of the memory is garbage (it only touches the nodes reachable from the root)
+ data is clustered together and memory is “de-fragmented” + data is clustered together and memory is “de-fragmented”
- Mark-Sweep: - Mark-Sweep:
requires a long pause in program execution requires a long pause in program execution
+ can handle cyclical data structures! + can handle cyclical data structures!
+ requires 1% extra memory (just one bit per node) + requires 1% extra memory (just one bit per node)
runs the same speed regardless of how much of memory is garbage. runs the same speed regardless of how much of memory is garbage.
It must touch all nodes in the mark phase, and must link together all garbage nodes into a free list. It must touch all nodes in the mark phase, and must link together all garbage nodes into a free list.
## 25.11 Practical Garbage Collection Methodology in C++: Smart Pointers ## 25.11 Practical Garbage Collection Methodology in C++: Smart Pointers
@@ -167,11 +170,11 @@ also when we are developing big complex programs that process and rearrange lots
enormous beast of a programming language (but that doesnt stop people from trying!). So is there anything enormous beast of a programming language (but that doesnt stop people from trying!). So is there anything
we can do? Yes, we can use Smart Pointers to gain some of the features of garbage collection. we can do? Yes, we can use Smart Pointers to gain some of the features of garbage collection.
Some examples below are modified from these nice online references: Some examples below are modified from these nice online references:
http://ootips.org/yonat/4dev/smart-pointers.html [http://ootips.org/yonat/4dev/smart-pointers.html](http://ootips.org/yonat/4dev/smart-pointers.html)
http://www.codeproject.com/KB/stl/boostsmartptr.aspx [http://www.codeproject.com/KB/stl/boostsmartptr.aspx](http://www.codeproject.com/KB/stl/boostsmartptr.aspx)
http://en.wikipedia.org/wiki/Smart_pointer [http://en.wikipedia.org/wiki/Smart_pointer](http://en.wikipedia.org/wiki/Smart_pointer)
http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm [http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm](http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm)
http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/ [http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/](http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/)
## 25.12 Whats a Smart Pointer? ## 25.12 Whats a Smart Pointer?
@@ -184,12 +187,12 @@ obsess about how & when it will get deleted (it happens automatically).
template <class T> template <class T>
class auto_ptr { class auto_ptr {
public: public:
explicit auto_ptr(T* p = NULL) : ptr(p) {} /* prevents cast/conversion */ explicit auto_ptr(T* p = NULL) : ptr(p) {} /* prevents cast/conversion */
~auto_ptr() { delete ptr; } ~auto_ptr() { delete ptr; }
T& operator*() { return *ptr; } T& operator*() { return *ptr; }
T* operator->() { return ptr; } /* fakes being a pointer */ T* operator->() { return ptr; } /* fakes being a pointer */
private: private:
T* ptr; T* ptr;
}; };
``` ```
@@ -197,17 +200,17 @@ T* ptr;
```cpp ```cpp
void foo() { void foo() {
Polygon* p(new Polygon(/* stuff */)); Polygon* p(new Polygon(/* stuff */));
p->DoSomething(); p->DoSomething();
delete p; delete p;
} }
``` ```
- Heres how we can re-write the same example with our auto_ptr: - Heres how we can re-write the same example with our auto_ptr:
```cpp ```cpp
void foo() { void foo() {
auto_ptr<Polygon> p(new Polygon(/* stuff */); auto_ptr<Polygon> p(new Polygon(/* stuff */);
p->DoSomething(); p->DoSomething();
} }
``` ```
@@ -230,7 +233,7 @@ std::vector<Polygon*> polys;
polys.push_back(new Triangle(/*...*/)); polys.push_back(new Triangle(/*...*/));
polys.push_back(new Quad(/*...*/)); polys.push_back(new Quad(/*...*/));
for (unsigned int i = 0; i < polys.size(); i++) { for (unsigned int i = 0; i < polys.size(); i++) {
delete polys[i]; delete polys[i];
} }
polys.clear(); polys.clear();
``` ```