|
1 | 1 | ---
|
2 | 2 | title: C26407
|
3 |
| -ms.date: 07/21/2017 |
| 3 | +ms.date: 08/18/2020 |
4 | 4 | ms.topic: "conceptual"
|
5 | 5 | f1_keywords: ["C26407"]
|
6 | 6 | helpviewer_keywords: ["C26407"]
|
7 | 7 | ms.assetid: 5539907a-bfa0-40db-82a6-b860c97209e1
|
8 | 8 | ---
|
9 | 9 | # C26407 DONT_HEAP_ALLOCATE_UNNECESSARILY
|
10 | 10 |
|
11 |
| -To avoid unnecessary use of pointers we try to detect common patterns of local allocations, for example when the result of a call to operator new is stored in a local variable and later explicitly deleted. This supports the rule R.5: *Prefer scoped objects, don't heap-allocate unnecessarily*. The suggested fix is to use an RAII type instead of a raw pointer and allow it to deal with resources. If an allocation is a single object, then it may be obviously unnecessary and a local variable of the object’s type would work better. |
| 11 | +To avoid unnecessary use of pointers, we try to detect common patterns of local allocations. For example, we detect when the result of a call to operator **`new`** is stored in a local variable and later explicitly deleted. This supports the [C++ Core Guidelines rule R.5](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r5-prefer-scoped-objects-dont-heap-allocate-unnecessarily): *Prefer scoped objects, don't heap-allocate unnecessarily*. To fix the issue, use an RAII type instead of a raw pointer, and allow it to deal with resources. Obviously, it isn't necessary to create a wrapper type to allocate a single object. Instead, a local variable of the object's type would work better. |
12 | 12 |
|
13 | 13 | ## Remarks
|
14 | 14 |
|
15 |
| -- To reduce the number of warnings, this pattern is detected for owner pointers only. So, it is necessary to mark owners properly first. We can easily extend this to cover raw pointers if we receive feedback from customers in support of such scenario. |
| 15 | +- To reduce the number of warnings, code analysis only detects this pattern for owner pointers. So, it's necessary to mark owners properly first. We can easily extend this to cover raw pointers if we receive [feedback](https://developercommunity.visualstudio.com/spaces/62/index.html) from customers in support of such scenarios. |
16 | 16 |
|
17 |
| -- The scoped object term may be a bit misleading, but the general idea is that we suggest using either a local variable whose lifetime is automatically managed, or a smart object which efficiently manages dynamic resources. Smart objects can of course do heap allocations, but it is not explicit in the code. |
| 17 | +- The *scoped object* term may be a bit misleading. In general, we suggest you use either a local variable whose lifetime is automatically managed, or a smart object that efficiently manages dynamic resources. Smart objects can of course do heap allocations, but it's not explicit in the code. |
18 | 18 |
|
19 |
| -- If the warning fires on array allocation (which is usually needed for dynamic buffers), the fix can be to use standard containers, or `std::unique_pointer<T[]>`. |
| 19 | +- If the warning fires on array allocation, (which is usually needed for dynamic buffers), you can fix it by using standard containers, or `std::unique_pointer<T[]>`. |
20 | 20 |
|
21 |
| -- The pattern is detected only for local variables, so we don’t warn on cases where an allocation is assigned to, say, a global variable and then deleted in the same function. |
| 21 | +- The pattern is detected only for local variables. We don’t warn in cases where an allocation is assigned to, say, a global variable and then deleted in the same function. |
22 | 22 |
|
23 | 23 | ## Example 1: Unnecessary object allocation on heap
|
24 | 24 |
|
|
0 commit comments