Any function that returns a resource that is not automatically released after use should have documentation on how to free the resource. In the case of malloc() it is documented as free() , for fopen() it is fclose() , etc.
When you create a function yourself, you can, for example, refer to free() if you return a pointer, which you, in turn, received from malloc() . If you have a more complicated setup, you may have to create your own function.
Looking at your function, you allocate memory using malloc() , then assign it to memory (or explode if the allocation fails, which you check too late), and then return exactly the pointer obtained from malloc() . Therefore, you are returning a resource that can (and should!) Be freed using free() .
By the way: think about not copying things unnecessarily. Your call to copy_int() seems superfluous to me, just return a pointer to const int, referring to the existing element, and you should be fine.
source share