Here's my problem: I read here on StackOverflow that it is unsafe sometimes to return pointers to local variables from a function. For example:
#include<iostream>
using namespace std;
int *foo(void) {
int x[] = {1,2,3};
return x;
}
int main() {
int *numbers;
numbers = foo();
return 0;
}
I'd like to know if this is unsafe, considering that x
being a local array, the memory could be unallocated, what's the better way to achieve the same result?