Hi everyone,
I am using a pointer to function to return a pointer of integers after certain calculations.
This pointer of integers is a 1D pointer (array), whose memory is allocated INSIDE the function that returns it.
Now, since I want to return this array to the main.c by a call of the type:
MYARRAY = function();
am I supposed to free the allocated memory inside the function or in main, even if in main I am not allocating MYARRAY?
I report a sample of my main.c and a sample of my function.c below.
Thank you in advance.
Best,
cfd
main.c
function.cCode:#include ... int main(int argc, char *argv[]) { int *MYARRAY; MYARRAY = function(); }
Code:int *function(void) { int n; int *ARRAY; //Allocation n = 100; ARRAY = calloc(n,sizeof(int *)); //Fill ARRAY[] for(i=0; i<n; i++) ARRAY[i] = i; //Freeing memory free(ARRAY); return ARRAY; }