I've created the following queue:
typedef struct
No {
int pos;
char cpf[12];
char nome[40];
struct No *prox;
} No;
typedef struct No * p_no;
typedef struct{
p_no ini, fim;
} Fila;
typedef Fila * p_fila;
And I have the following function to queue:
void enfileira(p_fila f, int pos, char cpf[], char nome[]) {
p_no novo;
novo = malloc(sizeof(No));
novo->pos = pos;
strcpy(novo->cpf, cpf);
strcpy(novo->nome, nome);
novo->prox = NULL;
if(f->ini == NULL)
f->ini = novo;
else
f->fim->prox = novo;
f->fim = novo;
}
However, when I run Valgrind to check for memory errors, it says that some blocks are being lost:
==16485== HEAP SUMMARY: ==16485== in use at exit: 600 bytes in 10 blocks ==16485== total heap usage: 19 allocs, 9 frees, 6,036 bytes allocated ==16485== ==16485== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 4 ==16485== at 0x482E27C: malloc (vg_replace_malloc.c:299) ==16485== by 0x108709: enfileira (in /home/student/Downloads/lab06) ==16485== by 0x108E83: main (in /home/student/Downloads/lab06) ==16485== ==16485== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4 ==16485== at 0x482E27C: malloc (vg_replace_malloc.c:299) ==16485== by 0x108709: enfileira (in /home/student/Downloads/lab06) ==16485== by 0x109245: main (in /home/student/Downloads/lab06)
I understand that it's because I'm not dealing with some part of the queue (I imagine). But I've already created the queue function and called it in main()
, but the error persists:
void libera_fila (p_fila f)
{
p_no t;
while(f->ini != NULL){
t = f->ini;
f->ini = f->ini->prox;
free(t);
}
free(f);
}