Skip to content

Commit f4e41f2

Browse files
authored
Merge pull request #4328 from corob-msft/docs/corob/cpp-docs-3952
Address cpp-docs 3952
2 parents 5172a8d + d3a7697 commit f4e41f2

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

docs/cpp/object-lifetime-and-resource-management-modern-cpp.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: "Object lifetime and resource management (RAII)"
33
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
55
ms.topic: "conceptual"
66
ms.assetid: 8aa0e1a1-e04d-46b1-acca-1d548490700f
77
---
88
# Object lifetime and resource management (RAII)
99

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.
1111

1212
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.
1313

@@ -48,7 +48,7 @@ public:
4848
};
4949
5050
void functionUsingWidget() {
51-
widget w(1000000); // lifetime automatically tied to enclosing scope
51+
widget w(1000000); // lifetime automatically tied to enclosing scope
5252
// constructs w, including the w.data member
5353
w.do_something();
5454
@@ -63,20 +63,19 @@ Since C++11, there's a better way to write the previous example: by using a smar
6363
class widget
6464
{
6565
private:
66-
std::unique_ptr<int> data;
66+
std::unique_ptr<int[]> data;
6767
public:
68-
widget(const int size) { data = std::make_unique<int>(size); }
68+
widget(const int size) { data = std::make_unique<int[]>(size); }
6969
void do_something() {}
7070
};
7171

7272
void functionUsingWidget() {
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
7575
// ...
7676
w.do_something();
7777
// ...
7878
} // automatic destruction and deallocation for w and w.data
79-
8079
```
8180

8281
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
8584

8685
## See also
8786

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)\
9089
[C++ Standard Library](../standard-library/cpp-standard-library-reference.md)

docs/cpp/welcome-back-to-cpp-modern-cpp.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Welcome back to C++ - Modern C++"
33
description: "Describes the new programming idioms in Modern C++ and their rationale."
4-
ms.date: 02/07/2022
4+
ms.date: 06/02/2022
55
ms.topic: "conceptual"
66
ms.assetid: 1cb1b849-ed9c-4721-a972-fd8f3dab42e2
77
---
@@ -24,15 +24,15 @@ To support easy adoption of RAII principles, the C++ Standard Library provides t
2424
class widget
2525
{
2626
private:
27-
std::unique_ptr<int> data;
27+
std::unique_ptr<int[]> data;
2828
public:
29-
widget(const int size) { data = std::make_unique<int>(size); }
29+
widget(const int size) { data = std::make_unique<int[]>(size); }
3030
void do_something() {}
3131
};
3232

3333
void functionUsingWidget() {
34-
widget w(1000000); // lifetime automatically tied to enclosing scope
35-
// constructs w, including the w.data gadget member
34+
widget w(1000000); // lifetime automatically tied to enclosing scope
35+
// constructs w, including the w.data gadget member
3636
// ...
3737
w.do_something();
3838
// ...

0 commit comments

Comments
 (0)