Is it correct to clean an internal malloc to a function with it?

1

Well given the following code is easy to do memory management:

Sample Code (in C):

#include <stdio.h> 
#include <stdlib.h> 

int *criarValor(void){

   int *valor  = malloc(sizeof *valor);

   *valor = 10;

   return valor;
}

int main(void)
{
   int *valorPtr;

   valorPtr = criarValor();

   /* Usa um ponteiro para acessar os valores */
   printf("O valor na heap é: %d\n", *valorPtr); 

   /* Libera memória alocada */
   free(valorPtr);

   return 0;
}

But if the dereferencing is done through the function itself? Is it possible (and / or necessary) to give the function free? Here is an example of what this would be like:

If in doubt (in C):

int *criarValor(void){

   int *valor  = malloc(sizeof *valor);

   *valor = 10;

   return valor;
}

int main(void)
{
   /* Usa a própria função dereferenciada para acessar os valores */
   printf("O valor na heap é: %d\n", *criarValor()); 

   /* É correto dar esse free? A memória alocada 
      é corretamente liberada? */
   free(criarValor());

   return 0;
}

Doubt continues in C ++:

Doubt Code (in C ++):

#include <iostream>

int *criarValor(void){

   int *valor  = new int(10);

   return valor;
}

int main()
{ 
   // Usa a própria função dereferenciada para acessar os valores
   std::cout << "O valor no heap é : " << *criarValor() << std::endl;

   // É correto dar esse deletada? A memória alocada 
   // é corretamente liberada?
   delete criarValor();
}

Is the allocated memory being properly released?

    
asked by anonymous 30.05.2015 / 17:45

1 answer

3
  

But if the dereferencing is done through the function itself?

When you do free(foo()) , you are not doing the free "through foo". You are simply giving free in the return value of foo (). It's as if you have done

 int *tmp = foo();
 free(tmp);

Similarly, in your printif you are not "using the pre-referenced function," you are deferring the pointer returned by the function.

An easy way to see this is to put a printf inside criarValor . You will see that you are running the function twice.

In C the best thing you can do is write the program as in your very first example. If you do not put the pointer declaration separate does not even look so bad:

int main(void)
{
   int *valorPtr = criarValor();

   /* Usa um ponteiro para acessar os valores */
   printf("O valor na heap é: %d\n", *valorPtr); 

   /* Libera memória alocada */
   free(valorPtr);

   return 0;
}

If your problem is that the middle of the code has more than one exit point, use goto:

int main(void)
{
   int *valorPtr = criarValor();

   if(valor == 17){
      /* Erro, abortar */
      goto cleanup;
   }else{
      printf("O valor na heap é: %d\n", *valorPtr); 
   }

  cleanup:
   free(valorPtr);

   return 0;
}

In C ++, you have one more option. Objects run their destructors as soon as they come out of scope and you can put the delete inside those destructors.

For example, you can put your pointer inside a smart pointer . In more modern versions of C ++ they are part of the standard library.

#include <memory>

int main()
{
   std::unique_ptr<int> valorPtr(criarValor());

   std::cout << "O valor no heap é : " << *valorPtr << std::endl;

   // Assim que essa função retorna e valorPtr sai do escopo,
   // o destrutor do unique_ptr roda e dá delete no conteúdo.
   return 0;
}
    
30.05.2015 / 21:35