Skip to content

Add C26515/C26816 warning documentation #4741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions docs/code-quality/c26815.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: c26815
keywords: c26815
author: Rastaban
ms.author: philc
ms.date: 01/27/2020
ms.topic: reference
ms.technology: vs-ide-code-analysis
f1_keywords:
- "c26815"
helpviewer_keywords:
- "c26815"
dev_langs: ["C++"]
manager: sunnych
---

# Warning C26815 The pointer is dangling because it points at a temporary instance which was destroyed.

There is a dangling pointer that is the result of an unnamed temporary that has been destroyed.

## Example

```cpp
std::optional<std::vector<int>> getTempOptVec();

void loop() {
// Oops, the std::optional value returned by getTempOptVec gets deleted
// because there is no reference to it.
for (auto i : *getTempOptVec()) // warning C26815
{
// do something interesting
}
}

void views()
{
// Oops, the 's' suffix turns the string literal into a temporary std::string.
std::string_view value("This is a std::string"s); // warning C26815
}
```

These issue can be fixed by extending the lifetime of the temporary object.

```cpp
std::optional<std::vector<int>> getTempOptVec();

void loop() {
// Fixed by extending the lifetime of the std::optional value by giving it a name.
auto temp = getTempOptVec();
for (auto i : *temp)
{
// do something interesting
}
}

void views()
{
// Fixed by changing to a constant string literal.
std::string_view value("This is a string literal");
}
```
50 changes: 50 additions & 0 deletions docs/code-quality/c26816.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: c26816
keywords: c26816
author: Rastaban
ms.author: philc
ms.date: 01/27/2020
ms.topic: reference
ms.technology: vs-ide-code-analysis
f1_keywords:
- "c26816"
helpviewer_keywords:
- "c26816"
dev_langs: ["C++"]
manager: sunnych
---

# Warning C26816 The pointer points to memory allocated on the stack

The pointer points to a variable that was allocated on the stack and will be cleaned up when it goes out of scope, thus invalidating the pointer.


## Example

```cpp
// In this example, std::string is being used internaly because the implementer felt it was easier to
// perform the non-trivial initialization of the value but the function returns a C-style string.
const char *danglingRawPtrFromLocal() {
std::string s;

// interesting string initialization here

return s.c_str(); // Oops, The pointer points to memory allocated on the stack
}
```

The fix is to eliminate the use of the pointer to the stack. This could be done by returning a value rather than a pointer
(in this case a std::string),
copying the data to the heap (Take care to verify it is correctly cleaned up though),
or adding an "out" variable to the function parameter list.

```cpp
std::string danglingRawPtrFromLocal() {
std::string s;

// interesting string initialization here

return s;
}
```

4 changes: 4 additions & 0 deletions docs/code-quality/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,10 @@
href: c26812.md
- name: C26814
href: c26814.md
- name: C26815
href: c26815.md
- name: C26816
href: c26816.md
- name: C/C++ warnings
items:
- name: Overview
Expand Down