I was trying to find the space complexity of the following code:
void f(int arr[], int n){
int m=0;
for(int i=1; i<n*n; ++i)
for(int k=0; k<n*n; k+=i){
m=(m>k)?m:k;
printf("1");
}
free(malloc(m));
}
the answer is n^2, however I don't quite understand how the memory allocation works here. I mean, why do we put the malloc inside the free? Doesn't it mean that we don't allocate any memory at all (because we free it right away? Or does it still count?).
Thanks!