Correct use of free () in function?

3

How to use free() function when it is used inside a function, in this function it generates a dynamic vector and itself will be the return of the function, eg:

int* copia(int *vet, int tam)
{
    int i, *retorno;
    retorno = (int*) malloc(tam * sizeof(int));

    for(i = 0; i < tam; i ++)
        retorno[i] = vet[i];
    return retorno;
};

My question is how to release the retorno vector properly, because Dev does not point to error, would it?

return retorno;
free(retorno);
In theory, you always have to release a dynamically allocated pointer and it should be released when its usage ends, but return is usually used to finalize a function, so I was intrigued by so, if I put free after return , does it mean that it did not release my vector?

Because in theory , after return the function to run, soon it would not read the free underneath it, but also can not put free before return , because I'm still using the vector in line return .

What's the right way?

    
asked by anonymous 05.12.2015 / 18:25

1 answer

5

The most correct way is not to do it that way. In C, the ideal is to always allocate and release the memory in the same function. So it looks like it's right, but the allocation should be made on the lowest level where it will be used.

If you insist on allocating this function, it will not cause any problems directly. But the function will have to document that that allocation should be released and you'll have to trust that it's done. This way it becomes asymmetrical, since the release can not be made in it, after all, if it is released, it can not return something that is being destroyed.

int* copia(int *vet, int tam) {
    int *retorno = malloc(tam * sizeof(int));
    for(int i = 0; i < tam; i++)
        retorno[i] = vet[i];
    return retorno; //está retornando porque será usado fora daqui
}

Call:

int *novo = copia(vet, tam);
//faz alguma coisa com o novo
free(novo); //aqui acaba o seu uso

See running on ideone .

If you follow the recommendation to keep the code more organized, it will look something like this:

void copia(int *vet, int tam, int *retorno) {
    for(int i = 0; i < tam; i++)
        retorno[i] = vet[i];
}

Call:

int *novo = malloc(tam * sizeof(int)); //aqui aloca o que esta função precisa usar
copia(vet, tam, novo); //não precisa retornar porque já passou por referência
//faz alguma coisa com o novo
free(novo); //aqui acaba o seu uso

See running on ideone .

The basic rule is that each role should be responsible for everything it needs. If it needs an allocated data, it must allocate it. If she has, she must release. It is easier to control this way and avoid memory leakage, right?

Read more about the subject .

In the question whether Dev is spoken. Would it be Dev C ++? If it is, I recommend using another compiler / IDE . This is very problematic and lets a lot of things go wrong, at least by default. It is meant to make it easier, but it also encourages learning wrong.

    
05.12.2015 / 18:45