|
1 | 1 | ---
|
2 | 2 | title: C26400
|
3 |
| -ms.date: 08/02/2017 |
4 |
| -ms.topic: "conceptual" |
| 3 | +description: "Describes the Microsoft C/C++ code analysis warning C26400, its causes, and how to address it." |
| 4 | +ms.date: 10/23/2020 |
5 | 5 | f1_keywords: ["C26400"]
|
6 | 6 | helpviewer_keywords: ["C26400"]
|
7 |
| -ms.assetid: b27e1c6d-8b52-40b3-9760-b93afef19c7a |
8 | 7 | ---
|
9 | 8 | # C26400 NO_RAW_POINTER_ASSIGNMENT
|
10 | 9 |
|
11 |
| -This check helps to enforce the *rule I.11: Never transfer ownership by a raw pointer (T\*)*, which is a subset of the rule *R.3: A raw pointer (a T\*) is non-owning.* Specifically, it warns on any call to operator `new` which saves its result in a variable of raw pointer type. It also warns on calls to functions that return `gsl::owner<T>` if their results are assigned to raw pointers. The idea here is that you should clearly state ownership of memory resources. For more information, see the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management). |
| 10 | +This check helps to enforce the *rule I.11: Never transfer ownership by a raw pointer (T\*)*, which is a subset of the rule *R.3: A raw pointer (a T\*) is non-owning*. Specifically, it warns on any call to `operator new`, which saves its result in a variable of raw pointer type. It also warns on calls to functions that return `gsl::owner<T>` if their results are assigned to raw pointers. The idea here is that you should clearly state ownership of memory resources. For more information, see the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management). |
12 | 11 |
|
13 |
| -The easiest way to fix this is to use **`auto`** declaration if the resource is assigned immediately at the variable declaration. If this is not possible, then we suggest that you use the type `gsl::owner<T>`. The **`auto`** declarations initialized with operator **`new`** are "owners" because we assume that the result of any allocation is implicitly an owner pointer. We transfer this assumption to the **`auto`** variable and treat it as `owner<T>`. |
| 12 | +The easiest way to fix this warning is to use **`auto`** declaration if the resource is assigned immediately at the variable declaration. If this fix isn't possible, then we suggest that you use the type `gsl::owner<T>`. The **`auto`** declarations initialized with operator **`new`** are "owners" because we assume that the result of any allocation is implicitly an owner pointer. We transfer this assumption to the **`auto`** variable and treat it as `owner<T>`. |
14 | 13 |
|
15 |
| -If this check flags a call to a function which returns `owner<T>`, this may be an indication of a legitimate bug in code. Basically, it points to a place where the code leaks an explicit notion of ownership (and maybe the resource itself). |
| 14 | +If this check flags a call to a function that returns `owner<T>`, it may be an indication of a legitimate bug in the code. Basically, it points to a place where the code leaks an explicit notion of ownership (and maybe the resource itself). |
16 | 15 |
|
17 | 16 | ## Remarks
|
18 | 17 |
|
19 |
| -This rule currently checks only local variables. If allocation is assigned to a formal parameter, global variable, class member, and so on, it is not flagged. Appropriate coverage of such scenarios is a part of future work. |
| 18 | +This rule currently checks only local variables. If you assign an allocation to a formal parameter, global variable, class member, and so on, it's not flagged. Appropriate coverage of such scenarios is planned for future work. |
20 | 19 |
|
21 | 20 | ## Example 1: Simple allocation
|
22 | 21 |
|
|
28 | 27 | buffer = new char[bufferSize]; // C26400
|
29 | 28 | ```
|
30 | 29 |
|
31 |
| -## Example 2: Simple allocation (fixed with gsl::owner\<T>) |
| 30 | +## Example 2: Simple allocation (fixed with `gsl::owner<T>`) |
32 | 31 |
|
33 | 32 | ```cpp
|
34 | 33 | gsl::owner<char*> buffer = nullptr;
|
35 | 34 | if (useCache)
|
36 | 35 | buffer = GetCache();
|
37 | 36 | else
|
38 | 37 | buffer = new char[bufferSize]; // OK
|
| 38 | +``` |
| 39 | + |
| 40 | +## Example 3: Simple allocation (fixed with `auto`) |
39 | 41 |
|
40 |
| -Example 3: Simple allocation (fixed with auto) |
| 42 | +```cpp |
41 | 43 | auto buffer = useCache ? GetCache() : new char[bufferSize]; // OK
|
42 | 44 | ```
|
0 commit comments