Skip to content

Commit 6c82190

Browse files
authored
Merge pull request #5445 from Rageking8/tweak-c4172-warning-reference
Tweak C4172 warning reference
2 parents 087e22d + 78e1890 commit 6c82190

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed
Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
---
2-
description: "Learn more about: Compiler Warning (level 1) C4172"
32
title: "Compiler Warning (level 1) C4172"
4-
ms.date: "11/04/2016"
3+
description: "Learn more about: Compiler Warning (level 1) C4172"
4+
ms.date: 06/25/2025
55
f1_keywords: ["C4172"]
66
helpviewer_keywords: ["C4172"]
7-
ms.assetid: a8d2bf65-d8b1-4fe3-8340-a223d7e7fde6
87
---
98
# Compiler Warning (level 1) C4172
109

11-
returning address of local variable or temporary
10+
> returning address of local variable or temporary : *optional_context*
11+
12+
## Remarks
1213

1314
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.
1415

1516
Redesign the function so that it does not return the address of a local object.
1617

17-
The following sample generates C4172:
18+
## Example
19+
20+
The following example generates C4172:
1821

1922
```cpp
2023
// C4172.cpp
21-
// compile with: /W1 /LD
22-
float f = 10;
24+
// compile with: /c /W1
25+
26+
const int* func1()
27+
{
28+
int i = 42;
29+
return &i; // C4172
30+
}
31+
32+
float f = 1.f;
2333

24-
const double& bar() {
25-
// try the following line instead
26-
// const float& bar() {
27-
return f; // C4172
34+
const double& func2()
35+
// Try one of the following lines instead:
36+
// const float& func2()
37+
// const auto& func2()
38+
{
39+
// The problem is that a temporary is created to convert f to a double.
40+
// C4172 in this case refers to returning the address of a temporary.
41+
return f; // C4172
2842
}
2943
```

docs/error-messages/compiler-warnings/compiler-warnings-c4000-through-c4199.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ The articles in this section describe Microsoft C/C++ compiler warning messages
138138
|[Compiler warning (level 1) C4166](compiler-warning-level-1-c4166.md)|illegal calling convention for constructor/destructor|
139139
|[Compiler warning (level 1) C4167](compiler-warning-level-1-c4167.md)|'*function*': only available as an intrinsic function|
140140
|[Compiler warning (level 1) C4168](compiler-warning-level-1-c4168.md)|compiler limit: out of debugger types, delete program database '*database*' and rebuild|
141-
|[Compiler warning (level 1) C4172](compiler-warning-level-1-c4172.md)|returning address of local variable or temporary *optional_context*|
141+
|[Compiler warning (level 1) C4172](compiler-warning-level-1-c4172.md)|returning address of local variable or temporary : *optional_context*|
142142
|[Compiler warning (level 1) C4174](compiler-warning-level-1-c4174.md)|'*name*': not available as a `#pragma component`|
143143
|[Compiler warning (level 1) C4175](compiler-warning-level-1-c4175.md)|`#pragma component(browser, on)`: browser info must initially be specified on the command line|
144144
|[Compiler warning (level 1) C4176](compiler-warning-level-1-c4176.md)|'*subcomponent*': unknown subcomponent for `#pragma component` browser|

0 commit comments

Comments
 (0)