-2

I am having problems freeing a struct, I have to create a dynamic array, read the number of arrays for further use and free the allocated memory.

typedef struct
{
  tContacto  contacto;  
  int        n_tiendas; 
  tTienda    *p_tiendas;   
}tCadena;

main()
{
   tCadena cadena;

   if (CrearTiendas(&cadena)==-1)
   //More code, no problem here

LiberaMemoria(cadena); //Function for freeing memory
return 0;
}

int CrearTiendas(tCadena *p_cadena)
{
    int numero;
    printf("Introduce el numero de tiendas:\t"); //Asking for number
    scanf("%d",&numero);
    if((p_cadena=(tCadena *)malloc(numero*sizeof(tCadena)))!=NULL)
    {
        return 0;   
    }
    else 
    {
        return -1;
    }

}

void LiberaMemoria(tCadena cadena)
{
    free(cadena); //Obviously this isn't correct, it's not a pointer
}

So, the only thing I can code myself is the LiberaMemoria() function. How can I correctly free the memory allocated on p_cadena?

Thank you.

Albermonte
  • 31
  • 5
  • 1
    `tCadena cadena;` is not allocate by `malloc` you must not `free` it. You must read a book of C before try to code something. – Stargateur Jan 07 '17 at 12:49
  • @GiuseppeLuigi Passing the address of the argument to `free()` would free the local copy (what is wrong) and not the passed object (what is wrong). – Amin Negm-Awad Jan 07 '17 at 12:49
  • @davmac Edited ;D – Albermonte Jan 07 '17 at 15:12
  • @Stargateur Please be gentle, obviously I have researched before on my book but still don't know how to do it, how should I free `p_cadena`? – Albermonte Jan 07 '17 at 15:14
  • p_cadena is a local variable in CrearTiendas. It does not exists outside of CrearTiendas, so if you allocate memory pointed to be p_acadena you must free it in CrearTiendas. – Stuart Jan 07 '17 at 16:12

2 Answers2

0

Your program is causing memory leak: your program is allocating some memory and throwing it away.

Change to local parameter p_cadena in the function CrearTiendas() will not affect local variables in main().

What should be done to your code is stop causing this memory leak: don't do the allcation just for causing memory leak.

int CrearTiendas(tCadena *p_cadena)
{
    int numero;
    printf("Introduce el numero de tiendas:\t"); //Asking for number
    scanf("%d",&numero);
    return 0;
}

Unfortunately, you say that you can edit only LiberaMemoria(). Then, do nothing for freeing the leaked memory and have the operating system free the memory.

void LiberaMemoria(tCadena cadena)
{
}

Reference: c - What REALLY happens when you don't free after malloc? - Stack Overflow

Community
  • 1
  • 1
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks but I need to create the dynamic array anyways, I have updated my post, hope now you understand it better. – Albermonte Jan 07 '17 at 15:15
0

The problem was on the memory allocation, the correct form would be:

p_cadena->p_tiendas=(tTienda *)malloc(numero*sizeof(tTienda)

and the free function:

void LiberaMemoria(tCadena cadena)
{
    free(cadena.p_tiendas);
}
Albermonte
  • 31
  • 5
  • So what about *So, the only thing I can code myself is the LiberaMemoria() function.*? – Amin Negm-Awad Jan 07 '17 at 18:55
  • @AminNegm-Awad I think I haven't explained well, I have to code both, `CrearTiendas()` and `LiberaMemoria()` but I cant change the definition or change `LiberaMemoria(tCadena cadena)` for `LiberaMemoria(tCadena *cadena)` as suggested before. Sorry for the misunderstanding. – Albermonte Jan 08 '17 at 00:33