How do I use free() and return the same content?

Asked

Viewed 117 times

4

I wanted to use the free() in ls_retorna in the following function, how could you do it with the best way?

char* MemCpyX(char *as_origem, int an_inicio, int an_quantidade)
{
    char *ls_retorno = (char*)malloc(an_quantidade);

    memcpy(ls_retorno, as_origem + an_inicio, an_quantidade);

    return(ls_retorno);
}
  • You want to use the free in which variable?

  • 1

    If you want to use free() in ls_retorno will not produce any useful results. If you want to use in as_origem, it is not a good idea to do there.

  • I was thinking about doing the return

  • memory is not allocated even after the end of the function?

  • No, free releases the allocated memory, in this case it should not be used

  • If doing this will kill what you just created, then it makes no sense to return that address. It might even work by coincidence which is the worst thing that can happen in a C code that "works". It’s allocated if it doesn’t work free(). To tell the truth the ideal is to give free() always where it allocates. Then you can think, so that’s where I’ll do it. The mistake is that the malloc() should not be there. The ideal is to allocate and dislocate at the place of use, leave the function only to do what is strictly necessary.

  • and change the ls_retorno by a simple vector? there would be no need to use free

  • But then you could not return it because it would cease to exist when the function closes.

  • I think I’ll do it by creating variable outside the function and passing the address as parameter, this would be a solution?

  • I think it would be better.

  • @bigown formulates that answer, which I accept she, helped a lot

Show 6 more comments

2 answers

3


It would be interesting to understand how stack and heap.

Understand why should we use the heap. Also: When to choose whether to use a pointer when creating an object?.

In general we must allocate and release a memory in the same function. Of course every "rule" can be violated if there is a good reason, but doing so helps to organize more and avoid complication in memory management and prevent leaks.

Allocate into stack, is easy, closed the scope, the time of life of it ends up and is destroyed (and at least it can be, so even if the die is still there you cannot use it reliably. An object created on the stack can only be guaranteed to be accessed within this scope, usually a function, or in the functions called by it. You can create an object there and pass a reference to it as an argument of a problem-free function call.

If you need a longer lifespan, which is to make an object survive the scope that was created, it has to be in the heap. The same is true if the object is potentially too large. If heap (probably with malloc()) have to release manually (probably with free(), or a function that has the free()), the ideal is to do in the same function to not lose control of what you must release (which may decrease a little one of its advantages).

So in the case of the question there is not much to solve optimally without taking away the allocation from it. If you allocate, as you are doing, and release within it, the memory becomes invalid and should no longer be accessed, so returning a pointer to the address of the allocated object is an error.

Leaving without releasing memory can work if you are sure who to call this function MemCpyX() will make the release. A danger, no?

It is best to allocate out, pass a reference to that allocated memory. Something like this:

void MemCpyX(char *as_origem, char *ls_retorno, int an_inicio, int an_quantidade) {
    memcpy(ls_retorno, as_origem + an_inicio, an_quantidade);
}

There you call it:

char *retorno = malloc(quantidade);
MemCpyX(origem, retorno, inicio, quantidade);
//faz alguma coisa
free(retorno);

I put in the Github for future reference.

Could also be:

char retorno[quantidade];
MemCpyX(origem, retorno, inicio, quantidade);

But let’s face it, so that function has become half-empty unless you intend to do something else there :)

  • when I call the function that way, error happens SIGSEGV, it wouldn’t be right MemCpyX(&origem, retorno, inicio, quantidade);?

  • It can be yes, it depends on how this variable was declared. I assumed it was a pointer, if it is not, then you have to do it that way.

  • and in fact I used this function as an example of my doubt, I have several different functions with the same format as it, but just to put as an example I used this meaningless function, thank you

  • @Lucasfernandes I imagined.

1

My opinion is that you should modify the function so you don’t need to use the free()

Follow a suggestion on how to do this:

char* MemCpyX(char *as_origem, char *ls_retorno, int an_inicio, int an_quantidade)
{
    memcpy(ls_retorno, as_origem + an_inicio, an_quantidade);
}

But then there would be no need for this function, since it could use the memcpy in place of MemCpyX

Browser other questions tagged

You are not signed in. Login or sign up in order to post.