This NEVER works, can never work.
Code:
int *foo ( ) {
int array[10];
// do stuff
return array;
}
Fix 1
Code:
int *foo ( ) {
static int array[10];
// do stuff
return array;
}
The array persists past the end of the function call, but the function is not re-entrant.
It isn't thread safe (at all), and you have to be careful to make use of the result before calling the function again.
Code:
int *a = foo();
int *b = foo(); // you just trashed a
Fix 2
Code:
int *foo (int *array, int size) {
// do stuff
return array;
}
This is the only way to go really.
It gives the caller the greatest flexibility to decide what to use to store the data, whether it be a local array or some malloc'ed pointer.