Correct use of free() in function?

Asked

Viewed 1,523 times

4

How to use function free() when it is used within a function, in that function Gero a dynamic vector and the function itself will be the return, example:

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 vector retorno correctly, because Dev does not point error, would be like this?

return retorno;
free(retorno);

In the theory you always have to release a dynamically allocated pointer and it must be released when it ends its use, but the return is usually used to finalize a function, so I was intrigued by this, because if I put the free after the return means he didn’t release my vector?

Because in the theory, after return the function stops rotating, so it would not read the free underneath it, but I can’t put the free before the return, because I’m still using the vector on the line return.

What’s the right way?

1 answer

6


The most correct way is not to do it this way. In C, the ideal is to always allocate and free the memory in the same function. So it looks right, but the allocation should be done at the lowest level where it will be used.

If you insist on allocating to this function, it will not cause any problems directly. But the function will have to document that that allocation must be released and you will have to trust that it is done. Thus it becomes asymmetrical, since the liberation cannot be made in it, after all, if it is released, it cannot 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
}

Calling for:

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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];
}

Calling for:

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

Read more on the subject.

In the question if it speaks in Dev. Would it be Dev C++? If it is, recommend to use another compiler/IDE. This is quite problematic and lets go a lot of wrong things, at least by default. It is done to facilitate but also encourages learning wrong.

  • Thanks for clearing the doubt! Yes, it’s Dev C++, I know he’s problematic, but since in college he’s the only one installed I joined him to do the work, I remembered a day at a test, printing 8 + 2 he thought it was 20.. I will take a look at the link, I have installed the Code::Blocks, but its compiler is not working, I will do a search on the error and go to use it or look at others link.

Browser other questions tagged

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