Free a pointer local to a function and return the pointer

I have a C code similar to this. How can I free the pointer and return it ? If I create another local pointer variable and assign

p

to that local pointer variable, it is showing a warning that local variable pointer cannot be returned. Note that I have a recursion so I want to free up the pointer in the function itself. Any workaround. Thanks in advance.

UPDATE: Yes, I know that it makes no sense and we cannot do both. I am asking for an alternative approach. The problem is if I don't free the pointer, it is consuming a lot of memory for high input values. I want to get rid of high memory usage.