返回局部变量或临时地址: optional_context
注解
某个函数会返回局部变量或临时对象的地址。 局部变量和临时对象在某个函数返回时会被销毁,因此返回的地址无效。
请重新设计该函数,使其不返回局部对象的地址。
示例:
以下示例生成 C4172:
// C4172.cpp
// compile with: /c /W1
const int* func1()
{
int i = 42;
return &i; // C4172
}
float f = 1.f;
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
}