tags:

views:

161

answers:

5

I know we should free any variable allocated with malloc, but what if I return it in a function? Something like this:

char *somefunction(int somearg){
    char *str;

    str=(char *)malloc(sizeof(char *));

    //some code

    return str;
}

Should I free str? How could I do that?

+9  A: 

You have two options: one, pass a char* to somefunction and use that instead of allocating within somefunction, or two, free the return value later on.

The first option:

char *somefunction(char *str, int somearg){

    //some code

    return str;
}

// Elsewhere...
char *str = (char *) malloc....;
somefunction(str, 123);
// Code...
free(str);

The second option:

char *somestr = somefunction(123);
// Do something...
free(somestr);

I personally suggest the first option, as it's a little easier to avoid leaking memory when it's not being allocated within arbitary functions.

Matthew Iselin
Good answer (and question), I stumbled upon the same problem recently. One question: What if it's difficult to determine beforehand how much memory the function's return value needs? Then it would seem more natural to allocate in the function; otherwise you'd need some error handling in the function for the case that the supplied memory space is too small (e.g. for returning a string of variable length).
sleske
A variable length string could justify having memory allocated within the function. I would be very hesitant about such code in my own project however, and rather prefer to explicitly specify a maximum length that is chosen based on the context and requirements of the code. What sort of variable length strings are you talking about? They couldn't possibly be more than several KB/MB that could be pre-allocated?
Matthew Iselin
+1  A: 

You should free all the allocated space but if you return its because you will use those memory space in other parts of the program, so after you use it you should free. See every place in the code that calls the function and free the space after you use the returned value.

Alex
+1  A: 

If you intend to return the address of the block you should not free() the block but instead rely on the calling code to free() it later. This is called onwership passing.

If you free it in the function and return the pointer the calling code will run into undefined behavior trying to access the already freed block.

sharptooth
+2  A: 

You free it when you have finished with it. There is no rule that says that the free() that matches a malloc() must be in the same function.

anon
so, I could do something like free(str) out of the function or free(somefunction)?
Hector Villalobos
Yes indeed. Just like you can do with malloc(), which is after all just another function.
anon
+1  A: 

This is a practice for some existing functions (strdup(), for instance) but is generally a bad idea. Requiring that a user be aware of what happens inside a function call is a bad requirement - think how many functions you use who's internals are hidden from you. Generally speaking, you will want to have a user pass in a buffer and size instead of allocating memory for them.

ezpz
Agreed, but what if it's difficult to decide beforehand how much space will be needed (e.g. dynamically creating a string to be returned). Then it would seem more natural (and simpler) for the function to allocate as much memory as it needs. What do you think?
sleske