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

2

I wanted to use free() in ls_retorna in the following function, how could I do it in 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);
}
    
asked by anonymous 27.07.2016 / 21:35

2 answers

2

It would be interesting to understand how stack and heap a>.

Understand why we should use heap . Also: When should I 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, any "rule" can be violated if there is a good reason, but doing so helps to organize more and avoid complications in memory management and avoid leaks.

If you allocate to stack , it's easy, you have closed the scope, (or at least it can be, so even though the data is still there it can not be used reliably.) An object created on the stack can only be accessed within this scope, usually a function, or in the You can create an object there and pass a reference to it as an argument to a function call without problems.

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

So in the case of the question there is not much to solve in an ideal way without taking away the allocation of 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 object is an error.

Leaving without releasing memory can work if you are sure that whoever calls this MemCpyX() function will release. A danger, is not it?

It is best to allocate away, pass a reference to this 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);
}

Here's how it goes:

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

It could also be:

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

But let's face it, so this function has been left with no reason to exist, unless you want to do something else there:

    
27.07.2016 / 23:24
0

My opinion is that you should modify the function so that you do not need to use free()

Here's a hint 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 there would be no need for this function since you could use memcpy instead of MemCpyX

    
27.07.2016 / 21:50