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 :)
You want to use the free in which variable?
– Pilati
If you want to use
free()
inls_retorno
will not produce any useful results. If you want to use inas_origem
, it is not a good idea to do there.– Maniero
I was thinking about doing the return
– Lucas Fernandes
memory is not allocated even after the end of the function?
– Lucas Fernandes
No, free releases the allocated memory, in this case it should not be used
– Pilati
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 givefree()
always where it allocates. Then you can think, so that’s where I’ll do it. The mistake is that themalloc()
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.– Maniero
and change the
ls_retorno
by a simple vector? there would be no need to use free– Lucas Fernandes
But then you could not return it because it would cease to exist when the function closes.
– Maniero
I think I’ll do it by creating variable outside the function and passing the address as parameter, this would be a solution?
– Lucas Fernandes
I think it would be better.
– Maniero
@bigown formulates that answer, which I accept she, helped a lot
– Lucas Fernandes