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.