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/const-and-volatile-pointers.md
+6-5
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
---
2
-
title: "const and volatile Pointers"
3
-
ms.date: "11/04/2016"
2
+
title: "const and volatile pointers"
3
+
ms.date: "11/19/2019"
4
4
helpviewer_keywords: ["volatile keyword [C++], and pointers", "pointers, and const", "pointers, and volatile", "const keyword [C++], volatile pointers"]
5
5
ms.assetid: 0c92dc6c-400e-4342-b345-63ddfe649d7e
6
6
---
7
-
# const and volatile Pointers
7
+
# const and volatile pointers
8
8
9
-
The [const](../cpp/const-cpp.md) and [volatile](../cpp/volatile-cpp.md) keywords change how pointers are treated. The **const** keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter.
9
+
The [const](const-cpp.md) and [volatile](volatile-cpp.md) keywords change how pointers are treated. The **const** keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter.
10
10
11
11
The **volatile** keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application. Therefore, the **volatile** keyword is useful for declaring objects in shared memory that can be accessed by multiple processes or global data areas used for communication with interrupt service routines.
Copy file name to clipboardExpand all lines: docs/cpp/how-to-create-and-use-ccomptr-and-ccomqiptr-instances.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
---
2
-
title: "How to: Create and Use CComPtr and CComQIPtr Instances"
2
+
title: "How to: Create and use CComPtr and CComQIPtr instances"
3
3
ms.custom: "how-to"
4
-
ms.date: "11/04/2016"
4
+
ms.date: "11/19/2019"
5
5
ms.topic: "conceptual"
6
6
ms.assetid: b0356cfb-12cc-4ee8-b988-8311ed1ab5e0
7
7
---
8
-
# How to: Create and Use CComPtr and CComQIPtr Instances
8
+
# How to: Create and use CComPtr and CComQIPtr instances
9
9
10
10
In classic Windows programming, libraries are often implemented as COM objects (or more precisely, as COM servers). Many Windows operating system components are implemented as COM servers, and many contributors provide libraries in this form. For information about the basics of COM, see [Component Object Model (COM)](/windows/win32/com/component-object-model--com--portal).
Copy file name to clipboardExpand all lines: docs/cpp/how-to-create-and-use-shared-ptr-instances.md
+10-10
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
---
2
-
title: "How to: Create and Use shared_ptr Instances"
2
+
title: "How to: Create and use shared_ptr instances"
3
3
ms.custom: "how-to"
4
-
ms.date: "05/22/2019"
4
+
ms.date: "11/19/2019"
5
5
ms.topic: "conceptual"
6
6
ms.assetid: 7d6ebb73-fa0d-4b0b-a528-bf05de96518e
7
7
---
8
-
# How to: Create and Use shared_ptr Instances
8
+
# How to: Create and Use shared_ptr instances
9
9
10
10
The `shared_ptr` type is a smart pointer in the C++ standard library that is designed for scenarios in which more than one owner might have to manage the lifetime of the object in memory. After you initialize a `shared_ptr` you can copy it, pass it by value in function arguments, and assign it to other `shared_ptr` instances. All the instances point to the same object, and share access to one "control block" that increments and decrements the reference count whenever a new `shared_ptr` is added, goes out of scope, or is reset. When the reference count reaches zero, the control block deletes the memory resource and itself.
11
11
12
12
The following illustration shows several `shared_ptr` instances that point to one memory location.
Whenever possible, use the [make_shared](../standard-library/memory-functions.md#make_shared) function to create a `shared_ptr` when the memory resource is created for the first time. `make_shared` is exception-safe. It uses the same call to allocate the memory for the control block and the resource, which reduces the construction overhead. If you don't use `make_shared`, then you have to use an explicit `new` expression to create the object before you pass it to the `shared_ptr` constructor. The following example shows various ways to declare and initialize a `shared_ptr` together with a new object.
The following example shows how to declare and initialize `shared_ptr` instances that take on shared ownership of an object that has already been allocated by another `shared_ptr`. Assume that `sp2` is an initialized `shared_ptr`.
`shared_ptr` is also helpful in C++ Standard Library containers when you're using algorithms that copy elements. You can wrap elements in a `shared_ptr`, and then copy it into other containers with the understanding that the underlying memory is valid as long as you need it, and no longer. The following example shows how to use the `remove_copy_if` algorithm on `shared_ptr` instances in a vector.
You can use `dynamic_pointer_cast`, `static_pointer_cast`, and `const_pointer_cast` to cast a `shared_ptr`. These functions resemble the `dynamic_cast`, `static_cast`, and `const_cast` operators. The following example shows how to test the derived type of each element in a vector of `shared_ptr` of base classes, and then copy the elements and display information about them.
@@ -108,8 +108,8 @@ You can pass a `shared_ptr` to another function in the following ways:
108
108
109
109
The following example shows how `shared_ptr` overloads various comparison operators to enable pointer comparisons on the memory that is owned by the `shared_ptr` instances.
title: "How to: Create and Use unique_ptr Instances"
2
+
title: "How to: Create and use unique_ptr instances"
3
3
ms.custom: "how-to"
4
4
ms.date: "11/19/2018"
5
5
ms.topic: "conceptual"
6
6
ms.assetid: 9a373030-e587-452f-b9a5-c5f9d58b7673
7
7
---
8
-
# How to: Create and Use unique_ptr Instances
8
+
# How to: Create and use unique_ptr instances
9
9
10
10
A [unique_ptr](../standard-library/unique-ptr-class.md) does not share its pointer. It cannot be copied to another `unique_ptr`, passed by value to a function, or used in any C++ Standard Library algorithm that requires copies to be made. A `unique_ptr` can only be moved. This means that the ownership of the memory resource is transferred to another `unique_ptr` and the original `unique_ptr` no longer owns it. We recommend that you restrict an object to one owner, because multiple ownership adds complexity to the program logic. Therefore, when you need a smart pointer for a plain C++ object, use `unique_ptr`, and when you construct a `unique_ptr`, use the [make_unique](../standard-library/memory-functions.md#make_unique) helper function.
11
11
12
12
The following diagram illustrates the transfer of ownership between two `unique_ptr` instances.
13
13
14
-

14
+

15
15
16
16
`unique_ptr` is defined in the `<memory>` header in the C++ Standard Library. It is exactly as efficient as a raw pointer and can be used in C++ Standard Library containers. The addition of `unique_ptr` instances to C++ Standard Library containers is efficient because the move constructor of the `unique_ptr` eliminates the need for a copy operation.
17
17
18
-
## Example
18
+
## Example 1
19
19
20
20
The following example shows how to create `unique_ptr` instances and pass them between functions.
These examples demonstrate this basic characteristic of `unique_ptr`: it can be moved, but not copied. "Moving" transfers ownership to a new `unique_ptr` and resets the old `unique_ptr`.
25
25
26
-
## Example
26
+
## Example 2
27
27
28
28
The following example shows how to create `unique_ptr` instances and use them in a vector.
In the range for loop, notice that the `unique_ptr` is passed by reference. If you try to pass by value here, the compiler will throw an error because the `unique_ptr` copy constructor is deleted.
33
33
34
-
## Example
34
+
## Example 3
35
35
36
36
The following example shows how to initialize a `unique_ptr` that is a class member.
You can use [make_unique](../standard-library/memory-functions.md#make_unique) to create a `unique_ptr` to an array, but you cannot use `make_unique` to initialize the array elements.
Copy file name to clipboardExpand all lines: docs/cpp/how-to-create-and-use-weak-ptr-instances.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
---
2
-
title: "How to: Create and Use weak_ptr Instances"
2
+
title: "How to: Create and use weak_ptr instances"
3
3
ms.custom: "how-to"
4
-
ms.date: "09/18/2019"
4
+
ms.date: "11/19/2019"
5
5
ms.topic: "conceptual"
6
6
ms.assetid: 8dd6909b-b070-4afa-9696-f2fc94579c65
7
7
---
8
-
# How to: Create and Use weak_ptr Instances
8
+
# How to: Create and use weak_ptr instances
9
9
10
-
Sometimes an object must store a way to access the underlying object of a `shared_ptr` without causing the reference count to be incremented. Typically, this situation occurs when you have cyclic references between `shared_ptr` instances.
10
+
Sometimes an object must store a way to access the underlying object of a [shared_ptr](../standard-library/shared-ptr-class.md) without causing the reference count to be incremented. Typically, this situation occurs when you have cyclic references between `shared_ptr` instances.
11
11
12
-
The best design is to avoid shared ownership of pointers whenever you can. However, if you must have shared ownership of `shared_ptr` instances, avoid cyclic references between them. When cyclic references are unavoidable, or even preferable for some reason, use `weak_ptr` to give one or more of the owners a weak reference to another `shared_ptr`. By using a `weak_ptr`, you can create a `shared_ptr` that joins to an existing set of related instances, but only if the underlying memory resource is still valid. A `weak_ptr` itself does not participate in the reference counting, and therefore, it cannot prevent the reference count from going to zero. However, you can use a `weak_ptr` to try to obtain a new copy of the `shared_ptr` with which it was initialized. If the memory has already been deleted, the `weak_ptr`'s bool operator returns `false`. If the memory is still valid, the new shared pointer increments the reference count and guarantees that the memory will be valid as long as the `shared_ptr` variable stays in scope.
12
+
The best design is to avoid shared ownership of pointers whenever you can. However, if you must have shared ownership of `shared_ptr` instances, avoid cyclic references between them. When cyclic references are unavoidable, or even preferable for some reason, use [weak_ptr](../standard-library/weak-ptr-class.md) to give one or more of the owners a weak reference to another `shared_ptr`. By using a `weak_ptr`, you can create a `shared_ptr` that joins to an existing set of related instances, but only if the underlying memory resource is still valid. A `weak_ptr` itself does not participate in the reference counting, and therefore, it cannot prevent the reference count from going to zero. However, you can use a `weak_ptr` to try to obtain a new copy of the `shared_ptr` with which it was initialized. If the memory has already been deleted, the `weak_ptr`'s bool operator returns `false`. If the memory is still valid, the new shared pointer increments the reference count and guarantees that the memory will be valid as long as the `shared_ptr` variable stays in scope.
C++ supports dynamic allocation and deallocation of objects using the [new](../cpp/new-operator-cpp.md) and [delete](../cpp/delete-operator-cpp.md) operators. These operators allocate memory for objects from a pool called the free store. The **new** operator calls the special function [operator new](../cpp/new-operator-cpp.md), and the **delete** operator calls the special function [operator delete](../cpp/delete-operator-cpp.md).
10
+
C++ supports dynamic allocation and deallocation of objects using the [new](new-operator-cpp.md) and [delete](delete-operator-cpp.md) operators. These operators allocate memory for objects from a pool called the free store. The **new** operator calls the special function [operator new](new-operator-cpp.md), and the **delete** operator calls the special function [operator delete](delete-operator-cpp.md).
11
11
12
12
The **new** function in the C++ Standard Library supports the behavior specified in the C++ standard, which is to throw a std::bad_alloc exception if the memory allocation fails. If you still want the non-throwing version of **new**, link your program with nothrownew.obj. However, when you link with nothrownew.obj, the default **operator new** in the C++ Standard Library no longer functions.
13
13
@@ -21,13 +21,13 @@ When a statement such as the following is encountered in a program, it translate
21
21
char *pch = newchar[BUFFER_SIZE];
22
22
```
23
23
24
-
If the request is for zero bytes of storage, **operator new** returns a pointer to a distinct object (that is, repeated calls to **operator new** return different pointers). If there is insufficient memory for the allocation request, **operator new** throws a std::bad_alloc exception, or returns **nullptr** if you have linked in non-throwing **operator new** support.
24
+
If the request is for zero bytes of storage, **operator new** returns a pointer to a distinct object (that is, repeated calls to **operator new** return different pointers). If there is insufficient memory for the allocation request, **operator new** throws a `std::bad_alloc` exception, or returns **nullptr** if you have linked in non-throwing **operator new** support.
25
25
26
26
You can write a routine that attempts to free memory and retry the allocation; see [_set_new_handler](../c-runtime-library/reference/set-new-handler.md) for more information. For more details on the recovery scheme, see the Handling insufficient memory section of this topic.
27
27
28
28
The two scopes for **operator new** functions are described in the following table.
29
29
30
-
### Scope for operator new Functions
30
+
### Scope for operator new functions
31
31
32
32
|Operator|Scope|
33
33
|--------------|-----------|
@@ -41,7 +41,6 @@ The global **operator new** function is called when the **new** operator is used
41
41
An **operator new** function defined for a class is a static member function (which cannot, therefore, be virtual) that hides the global **operator new** function for objects of that class type. Consider the case where **new** is used to allocate and set memory to a given value:
42
42
43
43
```cpp
44
-
// spec1_the_operator_new_function1.cpp
45
44
#include<malloc.h>
46
45
#include<memory.h>
47
46
@@ -77,7 +76,6 @@ Blanks *SomeBlanks = new Blanks;
77
76
The compiler supports member array **new** and **delete** operators in a class declaration. For example:
78
77
79
78
```cpp
80
-
// spec1_the_operator_new_function2.cpp
81
79
classMyClass
82
80
{
83
81
public:
@@ -99,11 +97,9 @@ int main()
99
97
100
98
### Handling insufficient memory
101
99
102
-
Testing for failed memory allocation can be done with code such as the following:
100
+
Testing for failed memory allocation can be done as shown here:
103
101
104
102
```cpp
105
-
// insufficient_memory_conditions.cpp
106
-
// compile with: /EHsc
107
103
#include<iostream>
108
104
usingnamespacestd;
109
105
#defineBIG_NUMBER 100000000
@@ -116,7 +112,7 @@ int main() {
116
112
}
117
113
```
118
114
119
-
There is another ways to handle failed memory allocation requests: write a custom recovery routine to handle such a failure, then register your function by calling the [_set_new_handler](../c-runtime-library/reference/set-new-handler.md) run-time function.
115
+
There is another way to handle failed memory allocation requests. Write a custom recovery routine to handle such a failure, then register your function by calling the [_set_new_handler](../c-runtime-library/reference/set-new-handler.md) run-time function.
120
116
121
117
## <a id="delete_operator"> </a> The delete operator
Only one of the preceding two forms can be present for a given class. The first form takes a single argument of type `void *`, which contains a pointer to the object to deallocate. The second form—sized deallocation—takes two arguments, the first of which is a pointer to the memory block to deallocate and the second of which is the number of bytes to deallocate. The return type of both forms is **void** (**operator delete** cannot return a value).
135
131
136
-
The intent of the second form is to speed up searching for the correct size category of the object to be deleted, which is often not stored near the allocation itself and likely uncached; the second form is particularly useful when an **operator delete** function from a base class is used to delete an object of a derived class.
132
+
The intent of the second form is to speed up searching for the correct size category of the object to be deleted, which is often not stored near the allocation itself and likely uncached. The second form is useful when an **operator delete** function from a base class is used to delete an object of a derived class.
137
133
138
-
The **operator delete** function is static; therefore, it cannot be virtual. The **operator delete** function obeys access control, as described in [Member-Access Control](../cpp/member-access-control-cpp.md).
134
+
The **operator delete** function is static; therefore, it cannot be virtual. The **operator delete** function obeys access control, as described in [Member-Access Control](member-access-control-cpp.md).
139
135
140
136
The following example shows user-defined **operator new** and **operator delete** functions designed to log allocations and deallocations of memory:
0 commit comments