strcpy itself does not allocate memory for the target string, so no, it does not need to be freed.
Of course, if something else allocated memory for it, then yes, that memory should eventually be freed, but this has nothing to do with strcpy .
This previous statement seems to be true, since your definition is an array of character pointers, not an array of characters:
char* buffer[LEN];
and this will almost certainly be done with:
buffer[n] = malloc (length);
It is a good idea to start thinking in terms of malloc'ed memory responsibility. By this I mean that transferring a malloc memory block may also include transferring responsibility for its release at some point.
You just need to find out (or decide if this is your code) whether it is responsible for managing the memory along with the memory itself. With strcpy , even if you go to an existing malloc'ed block for the recipient, responsibility will not be transferred, so you still have to free this memory yourself. This makes it easy to go to the malloc'ed or non-malloc'ed buffer without worrying about it.
You might be thinking of strdup , which basically creates a copy of the string, first allocating memory for it. The string returned from it must be freed, definitely.
paxdiablo
source share