I was clearing memory from previous mallocs and I got this error : Error in `./prot': double free or corruption (fasttop) . I allocated memory for some structs and I got this error when trying to free them. This are the structs I used:
struct ProdutoN
{
char prod[MAXBUFFPROD];
int altura;
struct ProdutoN *esq;
struct ProdutoN *dir;
};
struct ArrayProd
{
ProdutoNP lista[26];
int tamanho[26];
};
I created an incomplete type called CatalogoProdutos
that points to the struct called ArrayProd
. Inside that same struct I created an incomplete type called ProdutoNP
that points to the struct called ProdutoN
(which is an avl tree). This is how I have iniciated this structs :
void inicializa(CatalogoProdutos l)
{
int i;
l=(CatalogoProdutos)malloc(sizeof(struct ArrayProd));
for(i=0;i<26;i++)
{
l->lista[i] = (ProdutoNP)malloc(sizeof(struct ProdutoN));
l->lista[i] = NULL;
l->tamanho[i]=0;
}
}
This is how I tried to free this :
void freePNP (ProdutoNP lista)
{
if (lista == NULL)
return;
free (lista->prod);
freePNP (lista->esq);
freePNP (lista->dir);
free(lista);
}
void freeCP (CatalogoProdutos l)
{
int i;
if (l == NULL)
return;
for (i = 0 ; i < 26 ; i++)
freePNP(l->lista[i]);
free(l);
}
Definitions of the incomplete types:
typedef struct ProdutoN *ProdutoNP;
typedef struct ArrayProd *CatalogoProdutos;
What I am doing wrong ?