Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a string created with 'strcpy' need to be freed?

Tags:

c

strcpy

Does a string created with 'strcpy' need to be freed? And how to free it?

Edit: The destination is allocated like this:

char* buffer[LEN];
like image 907
Kristina Brooks Avatar asked Feb 20 '11 22:02

Kristina Brooks


People also ask

Does strcpy overwrite a string?

Once we call the strcpy() function, its content is overwritten with the new value of the source string. Hence, the strcpy() function does not append the content of the source string to the destination. Instead, it completely overwrites the destination string with the new value.

Why strcpy is not safe?

Problem with strcpy(): The strcpy() function does not specify the size of the destination array, so buffer overrun is often a risk. Using strcpy() function to copy a large character array into a smaller one is dangerous, but if the string will fit, then it will not be worth the risk.

Does strcpy copy memory?

No, strcpy knows nothing about memory, so your code copies a string into an uninitialized pointer pointing at la-la land.

Does strncpy overwrite?

strncpy overwrites existing character string.


2 Answers

strcpy itself doesn't allocate memory for the destination string so, no, it doesn't have to be freed.

Of course, if something else had allocated memory for it, then, yes, that memory should be freed eventually but that has nothing to do with strcpy.

That previous statement seems to be the case since your definition is an array of character pointers rather than an array of characters:

char* buffer[LEN];

and that will almost certainly be done with:

buffer[n] = malloc (length);

It's a good idea to start thinking in terms of responsibility for malloc'ed memory. By that, I mean passing a malloc'ed memory block may also involve passing the responsibility for freeing it at some point.

You just need to figure out (or decide, if it's your code) whether the responsibility for managing the memory goes along with the memory itself. With strcpy, even if you pass in an already-malloc'ed block for the destination, the responsibility is not being passed so you will still have to free that memory yourself. This allows you to easily pass in a malloc'ed or non-malloc'ed buffer without having to worry about it.

You may be thinking of strdup which is basically making a copy of a string by first allocating the memory for it. The string returned from that needs to be freed, definitely.

like image 197
paxdiablo Avatar answered Nov 09 '22 05:11

paxdiablo


If you use

char buffer[6];
strcpy(buffer, "hello");

for example, then buffer is freed when the end of its scope is reached.

On the other hand,

char *buffer;
buffer = malloc(sizeof(char) * 6);
strcpy(buffer, "hello");

this way you need to free the memory you allocated.

But it doesn't actually have anything to do with strcpy, only about how you allocate your string.

like image 20
ryuslash Avatar answered Nov 09 '22 05:11

ryuslash