1. any problem if i dynamically allocate memory and use only a small part of it?
2. can i free the dynamic memory after each time the function is executed?

1. No
2. Yes, providing you reallocate the memory before attempting to use it. .

is the position of 'free a' in pgm correct?

return a;
free a; //is this the correct position to free a?

No -- reverse the order of lines 5 and 6. As it is, line 6 will never get executed because the function exits on line 5.