-
Notifications
You must be signed in to change notification settings - Fork 967
Tweak C4172 warning reference #5445
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
Open
Rageking8
wants to merge
7
commits into
MicrosoftDocs:main
Choose a base branch
from
Rageking8:tweak-c4172-warning-reference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24bd1ba
Add blockquotes for C4172 warning message
Rageking8 9bc3a15
Add "Remarks" and "Example" headings in C4172 warning reference
Rageking8 dadf771
Tweak example in C4172 warning reference
Rageking8 10d9a37
Update metadata in C4172 warning reference
Rageking8 2acb092
Update compiler-warning-level-1-c4172.md
TylerMSFT 4380ed1
Tweak example in C4172 warning reference
Rageking8 78e1890
Update C4172 warning message again
Rageking8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 25 additions & 11 deletions
36
docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
--- | ||
description: "Learn more about: Compiler Warning (level 1) C4172" | ||
title: "Compiler Warning (level 1) C4172" | ||
ms.date: "11/04/2016" | ||
description: "Learn more about: Compiler Warning (level 1) C4172" | ||
ms.date: 06/20/2025 | ||
f1_keywords: ["C4172"] | ||
helpviewer_keywords: ["C4172"] | ||
ms.assetid: a8d2bf65-d8b1-4fe3-8340-a223d7e7fde6 | ||
--- | ||
# Compiler Warning (level 1) C4172 | ||
|
||
returning address of local variable or temporary | ||
> returning address of local variable or temporary | ||
|
||
## Remarks | ||
|
||
A function returns the address of a local variable or temporary object. Local variables and temporary objects are destroyed when a function returns, so the address returned is not valid. | ||
|
||
Redesign the function so that it does not return the address of a local object. | ||
|
||
The following sample generates C4172: | ||
## Example | ||
|
||
The following example generates C4172: | ||
|
||
```cpp | ||
// C4172.cpp | ||
// compile with: /W1 /LD | ||
float f = 10; | ||
// compile with: /c /W1 | ||
|
||
const int* func1() | ||
{ | ||
int i = 42; | ||
return &i; // C4172 | ||
} | ||
|
||
float f = 1.f; | ||
|
||
const double& bar() { | ||
// try the following line instead | ||
// const float& bar() { | ||
return f; // C4172 | ||
const double& func2() | ||
// Try one of the following lines instead: | ||
// const float& func2() | ||
// const auto& func2() | ||
{ | ||
// The problem is that a temporary is created to convert f to a double. | ||
// C4172 in this case refers to returning the address of a temporary. | ||
return f; // C4172 | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should standardize use of spaces over tabs and the 3 spaces before
// CXXXX
.