Skip to content

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
---
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
float f = 1.f;

const double& bar() {
// try the following line instead
// const float& bar() {
return f; // C4172
const double& func()
// Try one of the following lines instead:
// const float& func()
// const auto& func()
{
return f; // C4172
}
```