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

5

16

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];
Marrowfat answered 20/2, 2011 at 22:49 Comment(6)
What makes you think strcpy creates a string?Teriteria
And I hope that is not the declaration of buffer - either char* buffer or char buffer[LEN] - not both and should be LEN+1Jael
char * buffer [LEN]; allocates an array (with LEN size) of pointers to char. ¿Are you sure this is you want to use as strcpy destination?Itinerary
You might find it easier to convert your project to a garbage collected language such as Java or C#. Unless you are 100% comfortable with memory allocation/deallocation you are just asking for trouble trying to code in C/C++.Ferity
@James, C++ is now also a "garbage-collected language" if you use its smart pointers :-)Batting
@Batting wow! this is a question from 12 years ago. where does the time go. anyway it's 2023 so now surely everyone just uses Rust don't they ;)Ferity
B
25

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.

Batting answered 21/2, 2011 at 0:58 Comment(0)
H
8

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.

Huckleberry answered 21/2, 2011 at 7:13 Comment(0)
F
5

You provide a pointer to the destination buffer for strcpy, so it depends on how you allocated that buffer as to whether it needs to be freed and how to free it.

For example if you allocated the buffer using malloc, then yes you will need to free it. If you allocated the buffer on the stack then no you will not, it will be released automatically when it goes out of scope.

Ferity answered 20/2, 2011 at 22:51 Comment(0)
B
2

The strcpy function copies a string into a buffer you need to get some other way (such as malloc); you should free that buffer using whatever mechanism is correct for how you allocated it.

Beller answered 20/2, 2011 at 22:51 Comment(0)
A
1

strcpy() doesn't create a string, it only copies a string. Memory allocation is completely separated from that process.

So you have to take care of memory to which the string is copied - if it was allocated dynamically you have to free it at some point. Since you seem to have a stack-allocated buffer you don't have to do anything special - the buffer will be reclaimed when it goes out of scope.

Attachment answered 21/2, 2011 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.