You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cpp/object-lifetime-and-resource-management-modern-cpp.md
+9-10Lines changed: 9 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
title: "Object lifetime and resource management (RAII)"
3
3
description: "Follow the principle of RAII in modern C++ to avoid resource leaks."
4
-
ms.date: "11/19/2019"
4
+
ms.date: 06/02/2022
5
5
ms.topic: "conceptual"
6
6
ms.assetid: 8aa0e1a1-e04d-46b1-acca-1d548490700f
7
7
---
8
8
# Object lifetime and resource management (RAII)
9
9
10
-
Unlike managed languages, C++ doesn't have automatic *garbage collection*. That's an internal process that releases heap memory and other resources as a program runs. A C++ program is responsible for returning all acquired resources to the operating system. Failure to release an unused resource is called a *leak*. Leaked resources are unavailable to other programs until the process exits. Memory leaks in particular are a common cause of bugs in C-style programming.
10
+
Unlike managed languages, C++ doesn't have automatic *garbage collection*, an internal process that releases heap memory and other resources as a program runs. A C++ program is responsible for returning all acquired resources to the operating system. Failure to release an unused resource is called a *leak*. Leaked resources are unavailable to other programs until the process exits. Memory leaks in particular are a common cause of bugs in C-style programming.
11
11
12
12
Modern C++ avoids using heap memory as much as possible by declaring objects on the stack. When a resource is too large for the stack, then it should be *owned* by an object. As the object gets initialized, it acquires the resource it owns. The object is then responsible for releasing the resource in its destructor. The owning object itself is declared on the stack. The principle that *objects own resources* is also known as "resource acquisition is initialization," or RAII.
13
13
@@ -48,7 +48,7 @@ public:
48
48
};
49
49
50
50
void functionUsingWidget() {
51
-
widget w(1000000); // lifetime automatically tied to enclosing scope
51
+
widget w(1000000); // lifetime automatically tied to enclosing scope
52
52
// constructs w, including the w.data member
53
53
w.do_something();
54
54
@@ -63,20 +63,19 @@ Since C++11, there's a better way to write the previous example: by using a smar
63
63
classwidget
64
64
{
65
65
private:
66
-
std::unique_ptr<int> data;
66
+
std::unique_ptr<int[]> data;
67
67
public:
68
-
widget(const int size) { data = std::make_unique<int>(size); }
68
+
widget(const int size) { data = std::make_unique<int[]>(size); }
69
69
void do_something() {}
70
70
};
71
71
72
72
voidfunctionUsingWidget() {
73
-
widget w(1000000); // lifetime automatically tied to enclosing scope
74
-
// constructs w, including the w.data gadget member
73
+
widget w(1000000); // lifetime automatically tied to enclosing scope
74
+
// constructs w, including the w.data gadget member
75
75
// ...
76
76
w.do_something();
77
77
// ...
78
78
} // automatic destruction and deallocation for w and w.data
79
-
80
79
```
81
80
82
81
By using smart pointers for memory allocation, you may eliminate the potential for memory leaks. This model works for other resources, such as file handles or sockets. You can manage your own resources in a similar way in your classes. For more information, see [Smart pointers](smart-pointers-modern-cpp.md).
@@ -85,6 +84,6 @@ The design of C++ ensures objects are destroyed when they go out of scope. That
85
84
86
85
## See also
87
86
88
-
[Welcome back to C++](../cpp/welcome-back-to-cpp-modern-cpp.md)<br/>
89
-
[C++ Language Reference](../cpp/cpp-language-reference.md)<br/>
87
+
[Welcome back to C++](../cpp/welcome-back-to-cpp-modern-cpp.md)\
88
+
[C++ language reference](../cpp/cpp-language-reference.md)\
90
89
[C++ Standard Library](../standard-library/cpp-standard-library-reference.md)
0 commit comments