Skip to content
Merged
Changes from 5 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,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
}
```