Warning: Pointer Return Function

Hello,

I keep getting an warning because of this:

1
2
3
4
5
6
7
8
int* input()
{
    int temp[2];
    ifstream fin("data.in");
    fin >> temp[0] >> temp[1];
    fin.close();
    return &temp[0];
}



The warning I keep getting is this:

test.cpp: In function ‘int* input()’:
test.cpp:13: warning: address of local variable ‘temp’ returned


How can I get ride of that warning?
You are returning a pointer to something on the stack which is bad. Stack memory temporary and used over and over, so you can't expect that memory location to have the value any time in the future.
Last edited on
Topic archived. No new replies allowed.