You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Learn more about: Compiler Warning (level 1) C4172"
3
2
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
5
5
f1_keywords: ["C4172"]
6
6
helpviewer_keywords: ["C4172"]
7
-
ms.assetid: a8d2bf65-d8b1-4fe3-8340-a223d7e7fde6
8
7
---
9
8
# Compiler Warning (level 1) C4172
10
9
11
-
returning address of local variable or temporary
10
+
> returning address of local variable or temporary : *optional_context*
11
+
12
+
## Remarks
12
13
13
14
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.
14
15
15
16
Redesign the function so that it does not return the address of a local object.
16
17
17
-
The following sample generates C4172:
18
+
## Example
19
+
20
+
The following example generates C4172:
18
21
19
22
```cpp
20
23
// C4172.cpp
21
-
// compile with: /W1 /LD
22
-
float f = 10;
24
+
// compile with: /c /W1
25
+
26
+
constint* func1()
27
+
{
28
+
int i = 42;
29
+
return &i; // C4172
30
+
}
31
+
32
+
float f = 1.f;
23
33
24
-
constdouble& bar() {
25
-
// try the following line instead
26
-
// const float& bar() {
27
-
return f; // C4172
34
+
constdouble& 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.
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-warnings/compiler-warnings-c4000-through-c4199.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -138,7 +138,7 @@ The articles in this section describe Microsoft C/C++ compiler warning messages
138
138
|[Compiler warning (level 1) C4166](compiler-warning-level-1-c4166.md)|illegal calling convention for constructor/destructor|
139
139
|[Compiler warning (level 1) C4167](compiler-warning-level-1-c4167.md)|'*function*': only available as an intrinsic function|
140
140
|[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*|
142
142
|[Compiler warning (level 1) C4174](compiler-warning-level-1-c4174.md)|'*name*': not available as a `#pragma component`|
143
143
|[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|
0 commit comments