From 24bd1baa3abd4cdc8cb8192382fc2df7602f656d Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 20 Jun 2025 18:11:54 +0800 Subject: [PATCH 1/7] Add blockquotes for C4172 warning message --- .../compiler-warnings/compiler-warning-level-1-c4172.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index 45e27ccfbd..3131aa03a7 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -8,7 +8,7 @@ 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 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. From 9bc3a15a52b729ecfe28aca5be2170820fc014f9 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 20 Jun 2025 18:13:14 +0800 Subject: [PATCH 2/7] Add "Remarks" and "Example" headings in C4172 warning reference --- .../compiler-warnings/compiler-warning-level-1-c4172.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index 3131aa03a7..82155536f0 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -10,10 +10,14 @@ ms.assetid: a8d2bf65-d8b1-4fe3-8340-a223d7e7fde6 > 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. +## Example + The following sample generates C4172: ```cpp From dadf77144a4a85ccf671a7b7dfd6db52d94aaa7e Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 20 Jun 2025 18:23:23 +0800 Subject: [PATCH 3/7] Tweak example in C4172 warning reference --- .../compiler-warning-level-1-c4172.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index 82155536f0..11be7a98ed 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -18,16 +18,18 @@ Redesign the function so that it does not return the address of a local object. ## Example -The following sample generates C4172: +The following example generates C4172: ```cpp // C4172.cpp -// compile with: /W1 /LD -float f = 10; - -const double& bar() { -// try the following line instead -// const float& bar() { - return f; // C4172 +// compile with: /c /W1 +float f = 1.f; + +const double& func() +// Try one of the following lines instead: +// const float& func() +// const auto& func() +{ + return f; // C4172 } ``` From 10d9a37576ef8f22094047e67171fce990ac399a Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 20 Jun 2025 18:24:35 +0800 Subject: [PATCH 4/7] Update metadata in C4172 warning reference --- .../compiler-warnings/compiler-warning-level-1-c4172.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index 11be7a98ed..e2e00b8b21 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -1,10 +1,9 @@ --- -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 From 2acb092226806f6b9495bc31feefaa878a58379b Mon Sep 17 00:00:00 2001 From: Tyler Whitney Date: Tue, 24 Jun 2025 16:29:19 -0700 Subject: [PATCH 5/7] Update compiler-warning-level-1-c4172.md We demo an unusual case but not perhaps a more common case. Added it. --- .../compiler-warning-level-1-c4172.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index e2e00b8b21..0223340e50 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -22,13 +22,22 @@ The following example generates C4172: ```cpp // C4172.cpp // compile with: /c /W1 + +const int* func1() +{ + int i = 42; + return &i; // C4172 +} + float f = 1.f; -const double& func() +const double& func2() // Try one of the following lines instead: -// const float& func() -// const auto& func() +// 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 } ``` From 4380ed1ca929884cff9b15e0db35a8de8b766617 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 25 Jun 2025 17:04:00 +0800 Subject: [PATCH 6/7] Tweak example in C4172 warning reference --- .../compiler-warnings/compiler-warning-level-1-c4172.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index 0223340e50..d9238fbe95 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -26,7 +26,7 @@ The following example generates C4172: const int* func1() { int i = 42; - return &i; // C4172 + return &i; // C4172 } float f = 1.f; @@ -37,7 +37,7 @@ const double& 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 + // C4172 in this case refers to returning the address of a temporary. return f; // C4172 } ``` From 78e189054a5281330c7eb0ff5037e2421b66b081 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Wed, 25 Jun 2025 17:08:33 +0800 Subject: [PATCH 7/7] Update C4172 warning message again --- .../compiler-warnings/compiler-warning-level-1-c4172.md | 4 ++-- .../compiler-warnings-c4000-through-c4199.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md index d9238fbe95..393c35c0c6 100644 --- a/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md +++ b/docs/error-messages/compiler-warnings/compiler-warning-level-1-c4172.md @@ -1,13 +1,13 @@ --- title: "Compiler Warning (level 1) C4172" description: "Learn more about: Compiler Warning (level 1) C4172" -ms.date: 06/20/2025 +ms.date: 06/25/2025 f1_keywords: ["C4172"] helpviewer_keywords: ["C4172"] --- # Compiler Warning (level 1) C4172 -> returning address of local variable or temporary +> returning address of local variable or temporary : *optional_context* ## Remarks diff --git a/docs/error-messages/compiler-warnings/compiler-warnings-c4000-through-c4199.md b/docs/error-messages/compiler-warnings/compiler-warnings-c4000-through-c4199.md index 675357263c..83f1da2def 100644 --- a/docs/error-messages/compiler-warnings/compiler-warnings-c4000-through-c4199.md +++ b/docs/error-messages/compiler-warnings/compiler-warnings-c4000-through-c4199.md @@ -138,7 +138,7 @@ The articles in this section describe Microsoft C/C++ compiler warning messages |[Compiler warning (level 1) C4166](compiler-warning-level-1-c4166.md)|illegal calling convention for constructor/destructor| |[Compiler warning (level 1) C4167](compiler-warning-level-1-c4167.md)|'*function*': only available as an intrinsic function| |[Compiler warning (level 1) C4168](compiler-warning-level-1-c4168.md)|compiler limit: out of debugger types, delete program database '*database*' and rebuild| -|[Compiler warning (level 1) C4172](compiler-warning-level-1-c4172.md)|returning address of local variable or temporary *optional_context*| +|[Compiler warning (level 1) C4172](compiler-warning-level-1-c4172.md)|returning address of local variable or temporary : *optional_context*| |[Compiler warning (level 1) C4174](compiler-warning-level-1-c4174.md)|'*name*': not available as a `#pragma component`| |[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| |[Compiler warning (level 1) C4176](compiler-warning-level-1-c4176.md)|'*subcomponent*': unknown subcomponent for `#pragma component` browser|