This is not a question, but an interesting thing I discovered yesterday in visual studio c++ 17 compiler

27 Views Asked by At
int* func() {
    int c = 5;
    return &c;
}

int main() {
    int* s = func();
    std::cout << *(s) << "\n";
    *(s) = 1000;
    std::cout << *(s);
}

On my visual studio compiler this code works fine and giving me 5 and 1000. And I also checked it with 3 variables and discovered, that function keeps only one variable in memory for the lifetime of all program. So if anyone interested please check this code on your visual studio c++ 17 compiler. I just interested is it my compiler doesnt work fine or this is typical problem. Also in another compiler this code returns error as it must be.

0

There are 0 best solutions below