Udostępnij przez


Ostrzeżenie kompilatora (poziom 1) C4172

zwracany adres zmiennej lokalnej lub tymczasowej : optional_context

Uwagi

Funkcja zwraca adres zmiennej lokalnej lub obiektu tymczasowego. Zmienne lokalne i obiekty tymczasowe są niszczone po powrocie funkcji, więc zwrócony adres jest nieprawidłowy.

Przeprojektuj funkcję tak, aby nie zwracała adresu obiektu lokalnego.

Przykład

Poniższy przykład generuje 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
}