|
1 | 1 | ---
|
2 | 2 | title: C26409
|
3 |
| -ms.date: 07/21/2017 |
| 3 | +ms.date: 08/20/2020 |
4 | 4 | ms.topic: "conceptual"
|
5 | 5 | f1_keywords: ["C26409"]
|
6 | 6 | helpviewer_keywords: ["C26409"]
|
7 | 7 | ms.assetid: a3b3a229-d566-4be3-bd28-2876ccc8dc37
|
8 | 8 | ---
|
9 | 9 | # C26409 NO_NEW_DELETE
|
10 | 10 |
|
11 |
| -Even if code is clean of calls to malloc() and free() we still suggest that you consider better options than explicit use of operators [new and delete](/cpp/cpp/new-and-delete-operators). |
| 11 | +> `Avoid calling new and delete explicitly, use std::make_unique<T> instead (r.11).` |
12 | 12 |
|
13 |
| - **C++ Core Guidelines**: |
| 13 | +Even if code is clean of calls to` malloc()` and `free()`, we still suggest that you consider better options than explicit use of operators [`new` and `delete`](/cpp/cpp/new-and-delete-operators). |
| 14 | + |
| 15 | +**C++ Core Guidelines**:\ |
14 | 16 | [R.11: Avoid calling new and delete explicitly](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly)
|
15 | 17 |
|
16 |
| -The ultimate fix is to start using smart pointers with appropriate factory functions, such as [std::make_unique](/cpp/standard-library/memory-functions#make_unique). |
| 18 | +The ultimate fix is to use smart pointers and appropriate factory functions, such as [`std::make_unique`](/cpp/standard-library/memory-functions#make_unique). |
17 | 19 |
|
18 | 20 | ## Remarks
|
19 | 21 |
|
20 |
| -- The checker warns on calls to any kind of operator **`new`** or **`delete`**: scalar, vector, overloaded versions (global and class-specific), as well as on placement versions. The latter case may require some clarifications on the Core Guidelines in terms of suggested fixes and may be omitted in the future. |
| 22 | +- The checker warns on calls to any kind of operator **`new`** or **`delete`**: scalar, vector, overloaded versions (global and class-specific), and placement versions. The placement **`new`** case may require some clarifications in the Core Guidelines for suggested fixes, and may be omitted in the future. |
21 | 23 |
|
22 |
| -# Example |
23 |
| -```cpp |
| 24 | +## Example |
| 25 | + |
| 26 | +This example shows C26409 is raised for explicit **`new`** and **`delete`**. Consider using smart pointer factory functions such as `std::make_unique` instead. |
24 | 27 |
|
| 28 | +```cpp |
25 | 29 | void f(int i)
|
26 | 30 | {
|
27 |
| - int* arr = new int[i]{}; // C26409, warning is issued for all new calls |
28 |
| - delete[] arr; // C26409, warning is issued for all delete calls |
29 |
| - |
30 |
| - auto unique = std::make_unique<int[]>(i); // prefer using smart pointers over new and delete |
| 31 | + int* arr = new int[i]{}; // C26409, warning is issued for all new calls |
| 32 | + delete[] arr; // C26409, warning is issued for all delete calls |
| 33 | + |
| 34 | + auto unique = std::make_unique<int[]>(i); // prefer using smart pointers over new and delete |
31 | 35 | }
|
32 | 36 | ```
|
0 commit comments