Skip to content

Commit 975090d

Browse files
authored
Merge pull request #3228 from corob-msft/docs/corob/cpp-docs-2550
Address cpp-docs 2550 fix example
2 parents fa0d33b + 8c65af1 commit 975090d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

docs/code-quality/c26400.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
---
22
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
55
f1_keywords: ["C26400"]
66
helpviewer_keywords: ["C26400"]
7-
ms.assetid: b27e1c6d-8b52-40b3-9760-b93afef19c7a
87
---
98
# C26400 NO_RAW_POINTER_ASSIGNMENT
109

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).
1211

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>`.
1413

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).
1615

1716
## Remarks
1817

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

2120
## Example 1: Simple allocation
2221

@@ -28,15 +27,18 @@ else
2827
buffer = new char[bufferSize]; // C26400
2928
```
3029

31-
## Example 2: Simple allocation (fixed with gsl::owner\<T>)
30+
## Example 2: Simple allocation (fixed with `gsl::owner<T>`)
3231

3332
```cpp
3433
gsl::owner<char*> buffer = nullptr;
3534
if (useCache)
3635
buffer = GetCache();
3736
else
3837
buffer = new char[bufferSize]; // OK
38+
```
39+
40+
## Example 3: Simple allocation (fixed with `auto`)
3941

40-
Example 3: Simple allocation (fixed with auto)
42+
```cpp
4143
auto buffer = useCache ? GetCache() : new char[bufferSize]; // OK
4244
```

0 commit comments

Comments
 (0)